2
저는 컬렉션의 많은면을 사용할 수없는 교리 설정을했습니다. 사용Doctrine 2 컬렉션이 연결되지 않았거나 채워지지 않았습니다.
개체 : User.php :
class User extends \App\Entity
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
private $id;
/**
* @ManyToOne(targetEntity="Company", inversedBy="users")
*/
private $company;
public function getCompany()
{
return $this->company;
}
public function setCompany($company)
{
$this->company = $company;
}
}
Company.php :
class Company extends \App\Entity
{
/**
* @Id @Column(type="integer", nullable=false)
* @GeneratedValue
*/
private $id;
/**
* @OneToMany(targetEntity="User", mappedBy="company")
*/
private $users;
public function __construct()
{
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getUsers()
{
return $this->users;
}
}
내가 관계가 작동하는 것 같군 만들
. 오류가 발생하지 않습니다. 그럼 난 다음 시도 :$company = $this->em->getRepository('App\Entity\Company')->findOneByName("Walmart");
$user = $this->em->getRepository('App\Entity\User')->findOneByName("Niek");
$user->setCompany($company);
$company2 = $this->em->getRepository('App\Entity\Company')->findOneByName("Ford");
$user2 = $this->em->getRepository('App\Entity\User')->findOneByName("Henk");
$company2->getUsers()->add($user2);
$this->em->flush();
나는 첫 번째 사용자에 대한 데이터베이스를 검사하면 회사가 설정됩니다. 관계가 있습니다. 초는 지속되지 않습니다. 그리고 내가 이것을 할 때 :
print_r(\Doctrine\Common\Util\Debug::dump($company->getUsers(),$doctrineDepth));
print_r(\Doctrine\Common\Util\Debug::dump($company->getUsers2(),$doctrineDepth));
나는 2 개의 빈 배열을 얻는다.
그래서 배열이 연결되어 있지 않습니다. OneToMany ore ManyToOne 관계에서만 이와 같이 작동합니다. 한 ManyToMany 가지고 그 하나는 동일한 프로젝트에서 완벽하게 작동
어떤 아이디어?
그건 사실 회사를 저장합니다. 하지만 그것은 내 $ company-> getUsers()가 계속 비어있는 문제를 해결하지 못합니다. 그리고 그것은 양방향 관계의 목적을 무너 뜨릴 것입니다. –
Doctrine은 엔티티를 확장하고 관계를로드하기 위해'\ Doctrine \ ORM \ Proxy \ Proxy'를 구현하는 프록시 클래스를 만들려고하므로'extends \ App \ Entity'를 제거해보십시오. PHP는 다중 상속을 지원하지 않습니다. 또는 쿼리를 작성하여 시도하고 가져 오는 모든 관계를 포함하는지 확인하십시오. – Tomdarkness
프록시 클래스가 생성되지 않습니까? 모든 설정이 있습니다. 그리고 생성 된 파일이 하나 있습니다. 그것을 재생성 할 수 있도록 삭제했습니다. 그러나 아무 일도 일어나지 않습니다. –