I'm working on a bash script that create a simple project "from zero to CRUD" with some tool and Symfony 3 bin/console doctrine:generate:* It works fine but in the M:N association case i can't update data from the inverse side. I read some answers here and i started some tests but i'm confused among "cascade={"all"}" option, 'by_reference' => false and other suggests. What is the simplest way to do that starting from this basic example taken from the offical doctrine docs?
/** @Entity */
class User {
// ...
/**
* Many Users have Many Groups.
* @ManyToMany(targetEntity="Group", inversedBy="users")
*/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** @Entity */
class Group {
// ...
/**
* Many Groups have Many Users.
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
Here's a complete example of a many-to-many relationship in Symfony and Doctrine. I usually use yaml instead of annotations. So you'll have to convert yourself the code using annotations if you want using annotations.
I hope I didn't made any mistake, as I adapted my local
Category
-Product
example on yours.Next, you need to have, in each form type, a field to be able to select groups for a user, and users for a group.
Before updating the schema, you need to create both your entities, without any mapping. Then update the schema, then proceed with creating the many-to-many relation between the entities, and update the schema once again, to apply the relation. Give it a try, and let us know if it worked.