我有後續類單表繼承clausule實例來獲得查詢:Symfony2的學說:設定參數與
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="MyProject\MyBundle\Repository\ParameterRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"MidUpperArmCircumference" = "MyProject\MyBundle\Entity\MidUpperArmCircumference", "parameter" = "Parameter"})
*/
class Parameter{
...
}
,我試圖讓MidUpperCircumference實例:
...
$repository = $this->getDoctrine()->getRepository('MyprojectMyBundle:Parameter');
$discr = 'MyProject\MyBundle\Entity\MidUpperArmCircumference';
$midUpper = $repository->findOneByDiscr($discr);
我也有:
//MyProject\MyBundle\Repository\ParameterRepository
...
public function findOneByDiscr($discr)
{
$query = $this->getEntityManager()
->createQuery(
'SELECT u FROM MyProjectMyBundle:Parameter u
WHERE u INSTANCE OF :entity ')
->setParameter('entity', $discr);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
而我得到這個錯誤:
Warning: get_class() expects parameter 1 to be object, string given in D:\workspace\Project\Symfony2\vendor\doctrine\lib\Doctrine\ORM\Query\SqlWalker.php line 1727
你爲什麼要通過鑑別者查詢?它只能被Doctrine內部使用。參數實體的存儲庫只會查找屬於參數實體本身的記錄 – iBiryukov 2012-01-07 17:13:53
我使用getclass解決了這個問題。謝謝 – Munir 2012-01-13 17:00:31