2012-09-13 3 views
1

저는 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(); 

주어진 모든 선수를 검색 할 수 없습니다. 나는 광범위하게 교리 매뉴얼을 읽었고 내 뒤를 봤다가 지금 당황하고있다.

또한 오류 메시지가 나타나지 않아이 문제를 해결하는 데 어려움을 겪고 있습니다. 모든 포인터가 환영받을 것입니다.

+0

'플레이어'엔티티의 '$ team' 필드가 채워진 것처럼 보이고 구현이 [canonical] (http://docs.doctrine-project.org/en/2.1/reference/association- mapping.html). 'setPlayers()'메소드를 제공하면, 콜렉션의'var_dump'가 전달되도록합니다. – moonwave99

+0

'var_dump() 대신'\ Doctrine \ Common \ Util \ Debug :: dump();를 사용하십시오. –

+0

저의 참고로 표준적인 정의는 무엇입니까? 링크에서 찾을 수 없습니다. 나는 세터를 정의하는 것이 효과가있는 것 같지 않습니다. @Keyne 디버그 덤프를 시도하고 그 결과가 'string (4) "팀"'이 유용한 힌트가 될 것 같은데? – Pankrates

답변

-1

문제가 해결되었습니다. 저는 후손을 위해 해결책을 모으기 위해 노력했지만 오리지널 오류가 의심되는 파일을 더 이상 가지고 있지 않다는 결론에 도달했습니다. 나는 다른 프로젝트에서 파생 된 작업 사본을 얻을 수 있었다. 무차별 적으로 'diff'-ing하고 코드를 대체하여 영구 컬렉션의 빈 배열의 오류를 내 부트 스트랩 및 resources/doctrine.php 구성 파일에 추적했습니다.이 파일은 불행하게도 더 이상 필요 없기 때문에 이것을 확인할 수 없습니다 . 문자열 (4) "팀"이 여전히 (주석에서 논의 된대로) 반환되는 것을 당황스럽게 마침내 나는 교리 라이브러리 파일에 넣고 잊어 버린 다이 (die) 때문이라는 것을 알게되었습니다.

관련 문제