11
我有一個簡單的一對多關係,當我嘗試遍歷集合時出現錯誤。在Symfony 2中使用Doctrine訪問一對多關係錯誤
從 「一」 user.php的
/**
* @ORM\OneToMany(targetEntity="UserMeasurement", mappedBy="measurements")
*/
protected $measurements;
和相應的 「多」 UserMeasurement.php:
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="measurements", cascade={"persist"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
,然而當我嘗試從命令運行:
$query = $em->createQuery(" SELECT user FROM AcmeFooBundle:User user");
$users = $query->getResult();
foreach ($users as $user) {
print count($user->getMeasurements()->toArray());
}
我收到以下錯誤:
[ErrorException]
Notice: Undefined index: measurements in /Applications/MAMP/htdocs/Symfony/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1280
我運行了doctrine:schema:update --force
命令,它說我同步。我是不是迭代不正確?