2012-01-05 79 views
0

我有後續類單表繼承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

+0

你爲什麼要通過鑑別者查詢?它只能被Doctrine內部使用。參數實體的存儲庫只會查找屬於參數實體本身的記錄 – iBiryukov 2012-01-07 17:13:53

+0

我使用getclass解決了這個問題。謝謝 – Munir 2012-01-13 17:00:31

回答

0

要獲得MidUpperCircumference對象的情況下,你需要:

<?php 
use MyProject\MyBundle\Entity\MidUpperArmCircumference 

... 
$repository = $this->getDoctrine()->getRepository('MyprojectMyBundle:Parameter'); 
$discr = new MidUpperCircumference(); 
$midUpper = $repository->findOneByDiscr($discr); 
... 
0

您也可以嘗試設置您的參數(實體)的數據類型,如:

->setParameter('entity', $discr, \Doctrine\DBAL\Types\Type::OBJECT);