Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine 2 One-To-Many retrieving many side returns an empty collection
    primarykey
    data
    text
    <p>I am working on a project that utilizes Zend Framework 1.12 integrated with doctrine 2. I am having trouble with a Bidirectional One-to-Many relation in said project. The two entities concerning my problem are Team and Player; a team can have many players.</p> <p>The Team Entity:</p> <pre><code>namespace Entities; use Doctrine\Common\Collections\Collection, Doctrine\Common\Collections\ArrayCollection; /** * @Entity(repositoryClass="Repositories\TeamRepository") * @Table(name="team") */ class Team extends Entity{ /** * @Column(type="string", length=255) */ protected $name; /** * @OneToMany(targetEntity="Player", mappedBy="team") */ protected $players; public function __construct() { $this-&gt;players = new ArrayCollection(); } public function getName(){ return $this-&gt;name; } public function setName($name) { $this-&gt;name = $name; return $this; } public function getPlayers() { return $this-&gt;players; } </code></pre> <p>And the Player Entity:</p> <pre><code>namespace Entities; /** * @Entity(repositoryClass="Repositories\PlayerRepository") * @Table(name="player") */ class Player extends Entity{ public function __construct() { } /** * @Column(type="string", length=255) */ protected $name; /** * @ManyToOne(targetEntity="Team", inversedBy="players") * @JoinColumn(name="team_id", referencedColumnName="id") */ protected $team; public function getName(){ return $this-&gt;name; } public function setName($name) { $this-&gt;name = $name; return $this; } public function getTeam() { return $this-&gt;team; } public function setTeam($team) { $this-&gt;team = $team; return $this; } } </code></pre> <p>Now in my player controller for example I can retrieve a player and get the team name</p> <pre><code>$oPlayer = $this-&gt;_em-&gt;find('Entities\Player', $player_id); $teamname = $oPlayer-&gt;getTeam()-&gt;getName(); </code></pre> <p>This works as expected and I successfully obtain the name of the players team. However the other way around does not work. I can not retrieve all the players given a team</p> <pre><code>$oTeam = $this-&gt;_em-&gt;find('Entities\Team', $team_id); $oPlayers = $oTeam-&gt;getPlayers(); </code></pre> <p>When I var_dump this the result looks like</p> <pre><code>object(Doctrine\ORM\PersistentCollection)#238 (9) { ["snapshot":"Doctrine\ORM\PersistentCollection":private]=&gt; array(0) { } ["owner":"Doctrine\ORM\PersistentCollection":private]=&gt; object(Entities\Team)#195 (7) { ... } </code></pre> <p>Note that a persistenCollection seems to be build, however the array is empty. I have read the doctrine manual extensively and googled my behind off and am now at a loss.</p> <p>Also the fact that there is no error message, I am having a hard time solving this problem. Any pointers would be more than welcome.</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