Note that there are some explanatory texts on larger screens.

plurals
  1. POMany-to-Many, Doctrine's Entity Generator and Pluralization
    primarykey
    data
    text
    <p>Doctrine's many-to-many logic is confusing me a bit. I have a pretty simple many-to-many relationship of recipes to categories. My base entity classes are equally simple.</p> <p>The Recipe entity class...</p> <pre class="lang-php prettyprint-override"><code>class Recipe { /** * @ORM\ManyToMany(targetEntity="Category", inversedBy="categories") * @ORM\JoinTable(name="recipe_category") **/ private $categories; /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="title", type="string", length=255) */ private $title; public function __construct() { $this-&gt;categories = new \Doctrine\Common\Collections\ArrayCollection(); } } </code></pre> <p>And the Category entity class...</p> <pre class="lang-php prettyprint-override"><code>class Category { /** * @ORM\ManyToMany(targetEntity="Recipe") **/ private $recipes; /** * @var integer * * @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; public function __construct() { $this-&gt;recipes = new \Doctrine\Common\Collections\ArrayCollection(); } } </code></pre> <p>Seems pretty strait forward and matches Doctrine (and Symfony2's) documentation examples. The strange behavior comes when I try and generate the getters and setters for these classes via the Symfony console app.</p> <p>The relationship setters/getters are incorrect. Take, for instance, the Category setter in the Recipe class that's generated...</p> <pre class="lang-php prettyprint-override"><code>/** * Add categories * * @param \Namespace\CookbookBundle\Entity\Category $categories * @return Recipe */ public function addCategorie(\Namespace\CookbookBundle\Entity\Category $categories) { $this-&gt;categories[] = $categories; return $this; } </code></pre> <p>It looks like the auto-generation of the method name is off. It should be "addCategory" and should be passed a "category."</p> <p>While I can just correct this manually, if I re-run the entity generator, it will just add them again.</p> <p>Am I doing this incorrectly or is this just a quirk of the entity generator? Can I specify an over-ride via annotation?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload