简体   繁体   English

Doctrine 2一对多检索多方返回空集合

[英]Doctrine 2 One-To-Many retrieving many side returns an empty collection

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. 我正在开发一个使用与准则2集成的Zend Framework 1.12的项目。我在上述项目中遇到了双向一对多关系。 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 当我var_dump这个结果看起来像

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. 注意,似乎建立了一个persistenCollection,但是该数组为空。 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. 通过蛮力'diff'-ing和替换代码,我将持久性集合的空数组的错误跟踪到我的bootstrap和resources / doctrine.php配置文件中,不幸的是我没有了,因此无法确认这一点。 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. 令人尴尬的是,在仍以string(4)返回“团队”的情况下(如评论中所述),我终于发现这仅仅是由于die()导致了,我将其放入了理论库文件中而忘记了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM