我尝试使用 Doctrine 2 从数据库中获取数据,但是 findBy() 函数有问题。
我有实体用户:
/**
* @ORM\id
* @ORM\generatedValue
* @ORM\column(type="integer")
*/
private $id;
/**
* @ORM\column(type="string", length=32, unique=true)
*/
private $user;
和实体UserList:
/**
* @ORM\id
* @ORM\generatedValue(strategy="NONE")
* @ORM\oneToOne(targetEntity="User")
* @ORM\joinColumn(name="user_id", referencedColumnName="id")
*/
private $id;
/**
* @ORM\oneToOne(targetEntity="User")
* @ORM\joinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent = null;
我有数据表:
Users:
+----+------+
| id | user |
+----|------+
| 1 | John |
| 2 | Jane |
| 3 | Doe |
+----+------+
UsersList (id 3 have two parents):
+---------+-----------+
| user_id | parent_id |
+---------|-----------+
| 1 | NULL |
| 2 | NULL |
| 3 | 1 |
| 3 | 2 |
+---------+-----------+
我想得到 Doe (id 3) 的父母:
$user = $this->entityManager->getRepository('User')->findByUser('Doe');
$user_parents = $this->entityManager->getRepository('UserList')->findById($user);
foreach($user_parents as $parent)
{
var_dump($parent);
}
返回:
UserList(2) {
id private => 3
parent private => 1
}
UserList(2) {
id private => 3
parent private => 1 //this should be 2
}
问题出在哪里?谢谢。