Error with array collection with a one to many relation - arrays

I have a problem with a OneToMany relation and "array collection" between my entity CV and my entities, Formation, Skills and Experiences. A CV can have several formation, experiences or skills.
Even with Symfony documentation, I'm not even sure to get the issues.
Here is my Skills entity (formation and experiences are the same)
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\SkillsRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ApiResource()
* #ORM\Entity(repositoryClass=SkillsRepository::class)
*/
class Skills
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $type;
/**
* #ORM\Column(type="string", length=255)
*/
private $title;
/**
* #ORM\Column(type="string", length=255)
*/
private $level;
/**
* #ORM\ManyToOne(targetEntity=Cv::class, inversedBy="id_skills")
* #ORM\JoinColumn(nullable=false)
*/
private $id_cv;
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getLevel(): ?string
{
return $this->level;
}
public function setLevel(string $level): self
{
$this->level = $level;
return $this;
}
public function getIdCv(): ?Cv
{
return $this->id_cv;
}
public function setIdCv(?Cv $id_cv): self
{
$this->id_cv = $id_cv;
return $this;
}
}
And my CV entity:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\CvRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ApiResource()
* #ORM\Entity(repositoryClass=CvRepository::class)
*/
class Cv
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity=Formation::class, mappedBy="id_cv", orphanRemoval=true)
*/
private $id_formation;
/**
* #ORM\OneToMany(targetEntity=Experiences::class, mappedBy="id_cv", orphanRemoval=true)
*/
private $id_experiences;
/**
* #ORM\OneToMany(targetEntity=Skills::class, mappedBy="id_cv", orphanRemoval=true)
*/
private $id_skills;
/**
* #ORM\Column(type="string", length=255)
*/
private $title;
/**
* #ORM\Column(type="string", length=255)
*/
private $url_perso;
/**
* #ORM\Column(type="string", length=255)
*/
private $video;
/**
* #ORM\OneToOne(targetEntity=Candidate::class, mappedBy="id_cv", cascade={"persist", "remove"})
*/
private $id_candidate;
public function __construct()
{
$this->id_formation = new ArrayCollection();
$this->id_experiences = new ArrayCollection();
$this->id_skills = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* #return Collection|Formation[]
*/
public function getIdFormation(): Collection
{
return $this->id_formation;
}
public function addIdFormation(Formation $id_cv): self
{
if (!$this->id_formation->contains($id_cv)) {
$this->id_formation[] = $id_cv;
$id_cv->setIdCv($this);
}
return $this;
}
public function removeIdFormation(Formation $id_cv): self
{
if ($this->id_formation->removeElement($id_cv)) {
// set the owning side to null (unless already changed)
if ($id_cv->getIdCv() === $this) {
$id_cv->setIdCv(null);
}
}
return $this;
}
/**
* #return Collection|Experiences[]
*/
public function getIdExperiences(): Collection
{
return $this->id_experiences;
}
public function addIdExperience(Experiences $idExperience): self
{
if (!$this->id_experiences->contains($idExperience)) {
$this->id_experiences[] = $idExperience;
$idExperience->setIdCv($this);
}
return $this;
}
public function removeIdExperience(Experiences $idExperience): self
{
if ($this->id_experiences->removeElement($idExperience)) {
// set the owning side to null (unless already changed)
if ($idExperience->getIdCv() === $this) {
$idExperience->setIdCv(null);
}
}
return $this;
}
/**
* #return Collection|Skills[]
*/
public function getIdSkills(): Collection
{
return $this->id_skills;
}
public function addIdSkill(Skills $idSkill): self
{
if (!$this->id_skills->contains($idSkill)) {
$this->id_skills[] = $idSkill;
$idSkill->setIdCv($this);
}
return $this;
}
public function removeIdSkill(Skills $idSkill): self
{
if ($this->id_skills->removeElement($idSkill)) {
// set the owning side to null (unless already changed)
if ($idSkill->getIdCv() === $this) {
$idSkill->setIdCv(null);
}
}
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getUrlPerso(): ?string
{
return $this->url_perso;
}
public function setUrlPerso(string $url_perso): self
{
$this->url_perso = $url_perso;
return $this;
}
public function getVideo(): ?string
{
return $this->video;
}
public function setVideo(string $video): self
{
$this->video = $video;
return $this;
}
public function getIdCandidate(): ?Candidate
{
return $this->id_candidate;
}
public function setIdCandidate(?Candidate $id_candidate): self
{
// unset the owning side of the relation if necessary
if ($id_candidate === null && $this->id_candidate !== null) {
$this->id_candidate->setIdCv(null);
}
// set the owning side of the relation if necessary
if ($id_candidate !== null && $id_candidate->getIdCv() !== $this) {
$id_candidate->setIdCv($this);
}
$this->id_candidate = $id_candidate;
return $this;
}
}
In my CV entity, array collection and collection are underlined in red.I try to create my REST API.
When I do a post to create a CV, I have this error message:
Could not denormalize object of type
"Doctrine\Common\Collections\Collection", no supporting
normalizer found.
Do you have a clue about what's wrong?

Related

How to solve schema update error with doctrine?

I'm having an issue on a pretty simple application made with Symfony 4.3.4
I just updated an entity and I'm trying to update my database with "doctrine:schema:update --force", but I have here an issue : An exception occurred while executing 'ALTER TABLE lead ADD content LONGTEXT DEFAULT NULL'
Second message : Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lead ADD content LONGTEXT DEFAULT NULL' at line 1
I tried to change the name of the "content" attribute (it's the one I added), but this did not work. Do someone know why this is happening ?
Here is my Lead entity :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\LeadRepository")
*/
class Lead
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #ORM\Column(type="boolean")
*/
private $isAuth = false;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Formation", inversedBy="leads")
* #ORM\JoinColumn(nullable=false)
*/
private $formation;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $content;
public function __construct()
{
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getIsAuth(): ?bool
{
return $this->isAuth;
}
public function setIsAuth(bool $isAuth): self
{
$this->isAuth = $isAuth;
return $this;
}
public function getFormation(): ?Formation
{
return $this->formation;
}
public function setFormation(?Formation $formation): self
{
$this->formation = $formation;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
}
"lead" seems to be a reserved keyword for MySQL, so you need to escape it with backticks as follows:
/**
* #ORM\Entity(repositoryClass="App\Repository\LeadRepository")
* #ORM\Table(name="`lead`")
*/
You need to do this for other common keywords used as field names such as "order" and "sort" too.

Symfony 4 formbuilder loop each rows from database table

I have 2 entities :
Recettes
Categories
I try to build/list a form based on each rows of the table "Recettes" and display it from a controller.
Any ideas?
=======================
FORM_START
Name1 (TypeText) | Category (ChoiceType)
Name2 (TypeText) | Category (ChoiceType)
Name3 (TypeText) | Category (ChoiceType)
[Submit button]
FORM_END
=======================
Entity RECETTES
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Recettes
*
* #ORM\Table(name="recettes", indexes={#ORM\Index(name="categorie", columns={"categorie"})})
* #ORM\Entity
*/
class Recettes
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=50, nullable=false)
*/
private $nom;
/**
* #var \Categories
*
* #ORM\ManyToOne(targetEntity="Categories")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="categorie", referencedColumnName="id")
* })
*/
private $categorie;
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getCategorie(): ?Categories
{
return $this->categorie;
}
public function setCategorie(?Categories $categorie): self
{
$this->categorie = $categorie;
return $this;
}
}
Entity CATEGORIES
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categories
*
* #ORM\Table(name="categories")
* #ORM\Entity
*/
class Categories
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=50, nullable=false)
*/
private $nom;
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
}
In symfony/forms each Form is an EventDispatcherInterface.
You can subscribe to one of the Events your form fires 🔥
In your case you want to add fields before your form is beeing processed.
class FriendMessageFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$builder->add('activate_recette_1', ChoiceType::class);
$builder->add('activate_recette_2', ChoiceType::class);
});
}
}
You can inject your RecetteRepository via Dependency-Injection ans use the results.
Listen to the form events via:
Event-Listener
Event-Subscriber

Why there is 2 primary keys in table (Sentinel framework database)?

i included SENTINEL framework with implemented Sentinel migration into my LARAVEL project. Lot of things are clear to me but i can't understand why there is 2 primary keys in Role_users table, because i learned that for connection between 2 tables we need primary key and foreign key which is not a case here (role_users table). If someone knows explanation It would mean a lot to me.
my Database Scheme
Php my admin scheme
migration_cartalyst_sentinel
<?php
/**
* Part of the Sentinel package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* #package Sentinel
* #version 2.0.17
* #author Cartalyst LLC
* #license BSD License (3-clause)
* #copyright (c) 2011-2017, Cartalyst LLC
* #link http://cartalyst.com
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class MigrationCartalystSentinel extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('activations', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('code');
$table->boolean('completed')->default(0);
$table->timestamp('completed_at')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
});
Schema::create('persistences', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('code');
$table->timestamps();
$table->engine = 'InnoDB';
$table->unique('code');
});
Schema::create('reminders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('code');
$table->boolean('completed')->default(0);
$table->timestamp('completed_at')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('slug');
$table->string('name');
$table->text('permissions')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->unique('slug');
});
Schema::create('role_users', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->nullableTimestamps();
$table->engine = 'InnoDB';
$table->primary(['user_id', 'role_id']);
});
Schema::create('throttle', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->string('type');
$table->string('ip')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->index('user_id');
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('password');
$table->text('permissions')->nullable();
$table->timestamp('last_login')->nullable();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->unique('email');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('activations');
Schema::drop('persistences');
Schema::drop('reminders');
Schema::drop('roles');
Schema::drop('role_users');
Schema::drop('throttle');
Schema::drop('users');
}
}
EloquentUser
<?php
/**
* Part of the Sentinel package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* #package Sentinel
* #version 2.0.17
* #author Cartalyst LLC
* #license BSD License (3-clause)
* #copyright (c) 2011-2017, Cartalyst LLC
* #link http://cartalyst.com
*/
namespace Cartalyst\Sentinel\Users;
use Cartalyst\Sentinel\Permissions\PermissibleInterface;
use Cartalyst\Sentinel\Permissions\PermissibleTrait;
use Cartalyst\Sentinel\Persistences\PersistableInterface;
use Cartalyst\Sentinel\Roles\RoleableInterface;
use Cartalyst\Sentinel\Roles\RoleInterface;
use Illuminate\Database\Eloquent\Model;
class EloquentUser extends Model implements RoleableInterface, PermissibleInterface, PersistableInterface, UserInterface
{
use PermissibleTrait;
/**
* {#inheritDoc}
*/
protected $table = 'users';
/**
* {#inheritDoc}
*/
protected $fillable = [
'email',
'password',
'last_name',
'first_name',
'permissions',
];
/**
* {#inheritDoc}
*/
protected $hidden = [
'password',
];
/**
* {#inheritDoc}
*/
protected $persistableKey = 'user_id';
/**
* {#inheritDoc}
*/
protected $persistableRelationship = 'persistences';
/**
* Array of login column names.
*
* #var array
*/
protected $loginNames = ['email'];
/**
* The Eloquent roles model name.
*
* #var string
*/
protected static $rolesModel = 'Cartalyst\Sentinel\Roles\EloquentRole';
/**
* The Eloquent persistences model name.
*
* #var string
*/
protected static $persistencesModel = 'Cartalyst\Sentinel\Persistences\EloquentPersistence';
/**
* The Eloquent activations model name.
*
* #var string
*/
protected static $activationsModel = 'Cartalyst\Sentinel\Activations\EloquentActivation';
/**
* The Eloquent reminders model name.
*
* #var string
*/
protected static $remindersModel = 'Cartalyst\Sentinel\Reminders\EloquentReminder';
/**
* The Eloquent throttling model name.
*
* #var string
*/
protected static $throttlingModel = 'Cartalyst\Sentinel\Throttling\EloquentThrottle';
/**
* Returns an array of login column names.
*
* #return array
*/
public function getLoginNames()
{
return $this->loginNames;
}
/**
* Returns the roles relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(static::$rolesModel, 'role_users', 'user_id', 'role_id')->withTimestamps();
}
/**
* Returns the persistences relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function persistences()
{
return $this->hasMany(static::$persistencesModel, 'user_id');
}
/**
* Returns the activations relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activations()
{
return $this->hasMany(static::$activationsModel, 'user_id');
}
/**
* Returns the reminders relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function reminders()
{
return $this->hasMany(static::$remindersModel, 'user_id');
}
/**
* Returns the throttle relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function throttle()
{
return $this->hasMany(static::$throttlingModel, 'user_id');
}
/**
* Get mutator for the "permissions" attribute.
*
* #param mixed $permissions
* #return array
*/
public function getPermissionsAttribute($permissions)
{
return $permissions ? json_decode($permissions, true) : [];
}
/**
* Set mutator for the "permissions" attribute.
*
* #param mixed $permissions
* #return void
*/
public function setPermissionsAttribute(array $permissions)
{
$this->attributes['permissions'] = $permissions ? json_encode($permissions) : '';
}
/**
* {#inheritDoc}
*/
public function getRoles()
{
return $this->roles;
}
/**
* {#inheritDoc}
*/
public function inRole($role)
{
if ($role instanceof RoleInterface) {
$roleId = $role->getRoleId();
}
foreach ($this->roles as $instance) {
if ($role instanceof RoleInterface) {
if ($instance->getRoleId() === $roleId) {
return true;
}
} else {
if ($instance->getRoleId() == $role || $instance->getRoleSlug() == $role) {
return true;
}
}
}
return false;
}
/**
* {#inheritDoc}
*/
public function generatePersistenceCode()
{
return str_random(32);
}
/**
* {#inheritDoc}
*/
public function getUserId()
{
return $this->getKey();
}
/**
* {#inheritDoc}
*/
public function getPersistableId()
{
return $this->getKey();
}
/**
* {#inheritDoc}
*/
public function getPersistableKey()
{
return $this->persistableKey;
}
/**
* {#inheritDoc}
*/
public function setPersistableKey($key)
{
$this->persistableKey = $key;
}
/**
* {#inheritDoc}
*/
public function setPersistableRelationship($persistableRelationship)
{
$this->persistableRelationship = $persistableRelationship;
}
/**
* {#inheritDoc}
*/
public function getPersistableRelationship()
{
return $this->persistableRelationship;
}
/**
* {#inheritDoc}
*/
public function getUserLogin()
{
return $this->getAttribute($this->getUserLoginName());
}
/**
* {#inheritDoc}
*/
public function getUserLoginName()
{
return reset($this->loginNames);
}
/**
* {#inheritDoc}
*/
public function getUserPassword()
{
return $this->password;
}
/**
* Returns the roles model.
*
* #return string
*/
public static function getRolesModel()
{
return static::$rolesModel;
}
/**
* Sets the roles model.
*
* #param string $rolesModel
* #return void
*/
public static function setRolesModel($rolesModel)
{
static::$rolesModel = $rolesModel;
}
/**
* Returns the persistences model.
*
* #return string
*/
public static function getPersistencesModel()
{
return static::$persistencesModel;
}
/**
* Sets the persistences model.
*
* #param string $persistencesModel
* #return void
*/
public static function setPersistencesModel($persistencesModel)
{
static::$persistencesModel = $persistencesModel;
}
/**
* Returns the activations model.
*
* #return string
*/
public static function getActivationsModel()
{
return static::$activationsModel;
}
/**
* Sets the activations model.
*
* #param string $activationsModel
* #return void
*/
public static function setActivationsModel($activationsModel)
{
static::$activationsModel = $activationsModel;
}
/**
* Returns the reminders model.
*
* #return string
*/
public static function getRemindersModel()
{
return static::$remindersModel;
}
/**
* Sets the reminders model.
*
* #param string $remindersModel
* #return void
*/
public static function setRemindersModel($remindersModel)
{
static::$remindersModel = $remindersModel;
}
/**
* Returns the throttling model.
*
* #return string
*/
public static function getThrottlingModel()
{
return static::$throttlingModel;
}
/**
* Sets the throttling model.
*
* #param string $throttlingModel
* #return void
*/
public static function setThrottlingModel($throttlingModel)
{
static::$throttlingModel = $throttlingModel;
}
/**
* {#inheritDoc}
*/
public function delete()
{
$isSoftDeleted = array_key_exists('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this));
if ($this->exists && ! $isSoftDeleted) {
$this->activations()->delete();
$this->persistences()->delete();
$this->reminders()->delete();
$this->roles()->detach();
$this->throttle()->delete();
}
return parent::delete();
}
/**
* Dynamically pass missing methods to the user.
*
* #param string $method
* #param array $parameters
* #return mixed
*/
public function __call($method, $parameters)
{
$methods = ['hasAccess', 'hasAnyAccess'];
if (in_array($method, $methods)) {
$permissions = $this->getPermissionsInstance();
return call_user_func_array([$permissions, $method], $parameters);
}
return parent::__call($method, $parameters);
}
/**
* Creates a permissions object.
*
* #return \Cartalyst\Sentinel\Permissions\PermissionsInterface
*/
protected function createPermissions()
{
$userPermissions = $this->permissions;
$rolePermissions = [];
foreach ($this->roles as $role) {
$rolePermissions[] = $role->permissions;
}
return new static::$permissionsClass($userPermissions, $rolePermissions);
}
}
Role_users is used as a pivot table in this case; it's the intermediate table in the many-to-many relationship between Users and Roles. A User can belong to many Roles, and a Role can belong to many Users.
The composite primary key on Role_users ensures that no User will have the same Role twice, and vice versa. The primary key is a unique combination of the user_id and role_id.

Foreign key not stored in database using embed form in symfony 3

I have the following entities, form types and controller
Entity Client
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Client
*
* #ORM\Table(name="client")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ClientRepository")
*/
class Client
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #ORM\OneToMany(targetEntity="Contacts", mappedBy="client", cascade={"persist"})
*/
private $contacts;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Client
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
*
* #return Client
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Constructor
*/
public function __construct()
{
$this->contacts = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add contact
*
* #param \AppBundle\Entity\Contacts $contact
*
* #return Client
*/
public function addContact(\AppBundle\Entity\Contacts $contact)
{
$this->contacts[] = $contact;
return $this;
}
/**
* Remove contact
*
* #param \AppBundle\Entity\Contacts $contact
*/
public function removeContact(\AppBundle\Entity\Contacts $contact)
{
$this->contacts->removeElement($contact);
}
/**
* Get contacts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getContacts()
{
return $this->contacts;
}
}
Contacts entity is as below
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Contacts
*
* #ORM\Table(name="contacts")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ContactsRepository")
*/
class Contacts
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="fullname", type="string", length=255)
*/
private $fullname;
/**
* #var int
*
* #ORM\Column(name="user_type", type="smallint")
*/
private $userType;
/**
* #ORM\ManyToOne(targetEntity="Client", inversedBy="contacts")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set fullname
*
* #param string $fullname
*
* #return Contacts
*/
public function setFullname($fullname)
{
$this->fullname = $fullname;
return $this;
}
/**
* Get fullname
*
* #return string
*/
public function getFullname()
{
return $this->fullname;
}
/**
* Set userType
*
* #param integer $userType
*
* #return Contacts
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return int
*/
public function getUserType()
{
return $this->userType;
}
/**
* Set client
*
* #param \AppBundle\Entity\Client $client
*
* #return Contacts
*/
public function setClient(\AppBundle\Entity\Client $client = null)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* #return \AppBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
}
Client form type is as below
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\Client;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class ClientForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Client Name')))
->add('address', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Address')))
->add('contacts', CollectionType::class, array(
// each entry in the array will be an "email" field
'entry_type' => ContactsForm::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
))
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Client::class,
));
}
}
Contacts form type is as below
<?php
namespace AppBundle\Form;
use AppBundle\Entity\Contacts;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ContactsForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('fullname', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Full Name')))
->add('user_type', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Co-Owner'))
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Contacts::class,
));
}
}
Finally the controller to store data in two different tables being client as main table and contacts having foreign ken as client_id from table client is as below
/
**
* #Route("/client/add", name="add_client")
*/
public function addClientAction(Request $request)
{
$client = new Client();
$form = $this->createForm(ClientForm::class, $client);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$client = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($client);
$em->flush();
$this->addFlash(
'notice',
'Client Added!'
);
return $this->redirectToRoute('homepage');
}
return $this->render('default/addClient.html.twig', array('form' => $form->createView()));
}
The problem here is the data is added in both tables but the foreign key of the added client is not stored in table contacts, the value is null
This solution seems to work for some people may be for older version of symfony
But not luck to me. How to insert the foreign id as well. Am new to symfony and I am using symfony 3.2
It seems for me like a little mess up.
I guess you should add a contact from ContactController addAction.
Than in your Contact form type add EntityType, so you can choose a client for your contact. Here is some examples: http://symfony.com/doc/current/reference/forms/types/entity.html
Or you can set client manually by simple $contact->setClient($client) before you presist.
PS: rename Contacts entity to Contact. It is a little confusing)

Does Sonata admin work with self generated entity ids?

Does Sonata admin work self generated entity ids? I have the following entity which uses the Uuid library to generate its own id but when I try to create a new Group using Sonata admin it gets confused and thinks I am editing an existing entity and not creating a new one.
<?php declare(strict_types=1);
namespace App\Entity;
use App\Value\StartEndTime;
use App\Exception\GroupInactiveException;
use Ramsey\Uuid\Uuid;
use Assert\Assertion;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
class Group
{
/** #var string */
private $id;
/** #var string */
private $title;
/** #var string */
private $description;
/** #var User */
private $admin;
/** #var Collection */
private $members;
/** #var Collection */
private $invites;
/** #var StartEndTime */
private $startEndTime;
/** #var StartEndTime */
private $eventStartEndTime;
public function __construct()
{
$this->id = Uuid::uuid4()->toString();
$this->members = new ArrayCollection();
$this->invites = new ArrayCollection();
}
public function getId(): string
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function setTitle(string $title)
{
Assertion::notEmpty($title, 'Title is not specified');
$this->title = $title;
return $this;
}
public function getDescription()
{
return $this->description;
}
public function setDescription(string $description)
{
Assertion::notEmpty($description, 'Description is not specified');
$this->description = $description;
return $this;
}
public function getStartEndTime()
{
return $this->startEndTime;
}
public function setStartEndTime(StartEndTime $startEndTime)
{
$this->startEndTime = $startEndTime;
return $this;
}
public function getEventStartEndTime()
{
return $this->eventStartEndTime;
}
public function setEventStartEndTime(StartEndTime $startEndTime)
{
$this->eventStartEndTime = $startEndTime;
return $this;
}
}

Resources