저는 doctrine 2와 통합 된 Zend Framework 1.12를 사용하는 프로젝트에서 작업하고 있습니다. 프로젝트에서 양방향 일대 다 관계에 문제가 있습니다. 내 문제와 관련된 두 엔티티는 팀 및 플레이어입니다. 팀은 많은 선수를 보유 할 수 있습니다.Doctrine 2 여러면을 검색하는 일대 다 반환은 빈 모음을 반환합니다.
팀 엔티티 :
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;
}
그리고 플레이어 엔티티 :
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;
}
}
지금 예를 들어 내 플레이어 콘트롤러에 내가 선수를 검색하고 팀 이름을 얻을 수 있습니다
$oPlayer = $this->_em->find('Entities\Player', $player_id);
$teamname = $oPlayer->getTeam()->getName();
예상대로 작동하며 선수 팀의 이름을 성공적으로 얻었습니다. 그러나 그 반대의 경우는 작동하지 않습니다. 나는이 결과가 persistenCollection 구축 것 같다
같은object(Doctrine\ORM\PersistentCollection)#238 (9) {
["snapshot":"Doctrine\ORM\PersistentCollection":private]=>
array(0) {
}
["owner":"Doctrine\ORM\PersistentCollection":private]=>
object(Entities\Team)#195 (7) {
...
}
주를 보이는 위해서 var_dump 때 나는 그러나 배열이 비어, 팀
$oTeam = $this->_em->find('Entities\Team', $team_id);
$oPlayers = $oTeam->getPlayers();
주어진 모든 선수를 검색 할 수 없습니다. 나는 광범위하게 교리 매뉴얼을 읽었고 내 뒤를 봤다가 지금 당황하고있다.
또한 오류 메시지가 나타나지 않아이 문제를 해결하는 데 어려움을 겪고 있습니다. 모든 포인터가 환영받을 것입니다.
'플레이어'엔티티의 '$ team' 필드가 채워진 것처럼 보이고 구현이 [canonical] (http://docs.doctrine-project.org/en/2.1/reference/association- mapping.html). 'setPlayers()'메소드를 제공하면, 콜렉션의'var_dump'가 전달되도록합니다. – moonwave99
'var_dump() 대신'\ Doctrine \ Common \ Util \ Debug :: dump();를 사용하십시오. –
저의 참고로 표준적인 정의는 무엇입니까? 링크에서 찾을 수 없습니다. 나는 세터를 정의하는 것이 효과가있는 것 같지 않습니다. @Keyne 디버그 덤프를 시도하고 그 결과가 'string (4) "팀"'이 유용한 힌트가 될 것 같은데? – Pankrates