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.
The Team Entity:
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->players = new ArrayCollection();
}
public function getName(){
return $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getPlayers() {
return $this->players;
}
And the Player Entity:
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->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getTeam() {
return $this->team;
}
public function setTeam($team) {
$this->team = $team;
return $this;
}
}
Now in my player controller for example I can retrieve a player and get the team name
$oPlayer = $this->_em->find('Entities\Player', $player_id);
$teamname = $oPlayer->getTeam()->getName();
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
$oTeam = $this->_em->find('Entities\Team', $team_id);
$oPlayers = $oTeam->getPlayers();
When I var_dump this the result looks like
object(Doctrine\ORM\PersistentCollection)#238 (9) {
["snapshot":"Doctrine\ORM\PersistentCollection":private]=>
array(0) {
}
["owner":"Doctrine\ORM\PersistentCollection":private]=>
object(Entities\Team)#195 (7) {
...
}
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.
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.
The problem has been resolved. I have been trying to puzzle the solution together for posterity but have come to the conclusion that I no longer have the files where I now suspect the original error was located. I managed to get a working copy derived from another project. By brute force ‘diff’-ing and replacing code I traced the error of the empty array of the persistent collection, to my bootstrap and resources/doctrine.php config file, which unfortunately I do not have any longer and therefore can not confirm this. With string(4) “team” still being returned (as discussed in the comments) embarrassingly I finally found out that it was just due to a die() I put in the doctrine library file and forgot about.