Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo one-to-many relationship with reference table (Doctrine 2, ZF2)
    primarykey
    data
    text
    <p>I've a problem with my many-to-many relation. I want to have access to the reference table for a querybuilder query. With a many-to-many relation I don't have access to my reference table, so I've set up two one-to-many relationships. My structure look likes:</p> <p><code>User ---&gt; UserUserCategory &lt;--- UserCategory</code></p> <p>The above structure has two one-to-many relationships and are working fine with the database. When I have a user with the following data in the database (in UserUserCategory):</p> <pre><code>Table User ID | Name 1 | Bart 2 | Gerard Table Category ID | Name 1 | Officer 2 | Medic Table UserUserCategory User | Category 1 | 1 2 | 2 </code></pre> <p>So Bart is an Officer and Gerard is a Medic. But when I want to retrieve the data, it said that Bart is the Medic, and Gerard has a "null" value in the category. </p> <p><strong>My User-entity:</strong></p> <pre class="lang-php prettyprint-override"><code>/** * Entity Class representing a post of our User module. * * @ORM\Entity * @ORM\Table(name="user") * @ORM\Entity(repositoryClass="User\Repository\UserRepository") * */ class User extends zfcUser implements UserInterface { /** * Categories from user * * @ORM\OneToMany(targetEntity="User\Entity\UserUserCategory", mappedBy="user_id", cascade={"remove", "persist"}) * @var UserUserCategory * @access protected */ protected $user_usercategories; //name &amp; user_id comes here /** * Constructor to make a new ArrayCollection for addresses * * */ public function __construct() { $this-&gt;user_usercategories = new ArrayCollection(); } /** * @param Collection $categories */ public function addUserUserCategories(Collection $user_usercategories) { foreach ($user_usercategories as $user_usercategorie) { $user_usercategorie-&gt;setUser($this); $this-&gt;user_usercategories-&gt;add($user_usercategorie); } } /** * @param Collection $categories */ public function removeUserUserCategories(Collection $user_usercategories) { foreach ($user_usercategories as $user_usercategorie) { $user_usercategorie-&gt;setUser(null); $this-&gt;user_usercategories-&gt;removeElement($user_usercategorie); } } /** * @return Collection */ public function getUserUserCategories() { return $this-&gt;categories; } } </code></pre> <p><strong>My UserCategory-entity:</strong></p> <pre class="lang-php prettyprint-override"><code>/** * A User category entity. * @ORM\Entity * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_name_parentId", columns={"name", "parent_id"})}) * @ORM\HasLifecycleCallbacks */ class UserCategory extends Category { /** * User_usercategories * * @ORM\OneToMany(targetEntity="User\Entity\UserUserCategory", mappedBy="category_id") * @var UserUserCategory * @access protected */ protected $user_usercategories; /** * Constructor */ public function __construct() { $this-&gt;user_usercategories = new ArrayCollection(); } /** * @param Collection $categories */ public function addUserUserCategories(Collection $user_usercategories) { foreach ($user_usercategories as $user_usercategorie) { $user_usercategorie-&gt;setCategory($this); $this-&gt;user_usercategories-&gt;add($user_usercategorie); } } /** * @param Collection $categories */ public function removeUserUserCategories(Collection $user_usercategories) { foreach ($user_usercategories as $user_usercategorie) { $user_usercategorie-&gt;setCategory(null); $this-&gt;user_usercategories-&gt;removeElement($user_usercategorie); } } /** * @return Collection */ public function getUserUserCategories() { return $this-&gt;categories; } } </code></pre> <p><strong>My UserUserCategory-entity:</strong></p> <pre class="lang-php prettyprint-override"><code>/** * Entity Class representing a post of our User_UserCategory entity. * * @ORM\Entity * @ORM\Table(name="user_usercategory") * */ class UserUserCategory { /** * User with a category * * @ORM\ManyToOne(targetEntity="User\Entity\User", inversedBy="user_usercategories") * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", nullable=false, onDelete="CASCADE") * @ORM\Id * * @var User * @access protected */ protected $user_id; /** * Category from user * * @ORM\ManyToOne(targetEntity="User\Entity\UserCategory", inversedBy="user_usercategories") * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") * @ORM\Id * * @var Category * @access protected */ protected $category_id; public function getUser() { return $this-&gt;user; } /** * Set User * * @param User $user * @return User */ public function setUser(User $user = null) { //die('setUser'); $this-&gt;user = $user; return $this; } public function getCategory() { return $this-&gt;category; } /** * Set Category * * @param Category $category * @return Category */ public function setCategory(Category $category = null) { $this-&gt;category = $category; return $this; } } </code></pre> <p>When I execute the following line, it gives back the wrong result. The wrong category pops up:</p> <p>\Doctrine\Common\Util\Debug::dump($this->getEntityManager()->find('User\Entity\User', '49')->user_usercategories); die;</p> <p>Result:</p> <pre class="lang-php prettyprint-override"><code>array(1) { [0]=&gt; object(stdClass)#452 (3) { ["__CLASS__"]=&gt; string(28) "User\Entity\UserUserCategory" ["user_id"]=&gt; string(16) "User\Entity\User" ["category_id"]=&gt; string(24) "User\Entity\UserCategory" } } </code></pre> <p>In the category_id is the medic printed, I expect the officer to get back. </p> <p>In my other user, (id=60) the category_id field is "null". So it look likes Doctrine skips the first input in my UserUserCategory, starts with the second and can't get the last category anymore. </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.
    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