2012-11-12 34 views
0

我有兩個實體之間的關聯:從相關實體的Symfony查詢字段

Labs\CatalogBundle\Entity\ProductExtraspecs: 
type: entity 
table: orders__regmat__extraspecs 

fields: 
    regmatid: 
     id: true 
     type: integer 
     scale: 11 
    specid: 
     id: true 
     type: integer 
     scale: 11 
    attrib: 
     type: string 
     length: 20 
    value: 
     type: string 
     length: 200 

lifecycleCallbacks: { } 


manyToOne: 
    specs: 
    targetEntity: Specs 
    inversedBy: specs 
    joinColumn: 
     name: specid 
     referencedColumnName: specid 

而且

Labs\CatalogBundle\Entity\Specs: 
    type: entity 
    table: orders__regmat__extraspecs__specs 
    fields: 
     specid: 
      id: true 
      type: integer 
      unsigned: false 
      nullable: false 
      generator: 
       strategy: IDENTITY 
     desc: 
      type: string 
      length: 200 
      fixed: false 
      nullable: false 
     cat: 
      type: integer 
      unsigned: false 
      nullable: false 
     type: 
      type: integer 
      unsigned: false 
      nullable: false 

    lifecycleCallbacks: { } 


    oneToMany: 
     specs: 
      targetEntity: ProductExtraspecs 
      mappedBy: specs 

所以在控制器我想用regmatid = 8cat = 0所有行和tryed這一點:

$selRepository = $this -> getDoctrine() ->getEntityManager()->getRepository('LabsCatalogBundle:ProductExtraspecs'); 
    $query = $selRepository->createQueryBuilder('sel') 
         //->select('count(c.protid) as cnt') 
         ->where("sel.cat = '0' AND sel.regmatid = $id") 
         //->setParameter('price', '19.99') 
         ->getQuery(); 
    $selected = $query->getResult(); 

但是得到一個錯誤說:

[Semantical Error] line 0, col 74 near 'cat = '0' AND': Error: Class Labs\CatalogBundle\Entity\ProductExtraspecs has no field or association named cat

如何訪問關聯實體的字段? 你能幫我嗎?

預先感謝您:)

回答

0

你將不得不加入規格表:

$selRepository = $this->getDoctrine()->getRepository('LabsCatalogBundle:ProductExtraspecs'); 
$query = $selRepository->createQueryBuilder('sel') 
    ->leftJoin('sel.spec', 'spec') 
    ->where("spec.cat = '0' AND sel.regmatid = $id") 
    ->setParameter('price', '19.99') 
    ->getQuery(); 
$selected = $query->getResult(); 
+0

謝謝你完蛋了..但順便說一下..這'$ selected'被創造是在一個窗體中設置爲'實體字段類型'中的數據...但它不工作..你有任何消息嗎? Tahnk you again .. –

+0

順便說一句,最好是在你的倉庫中創建一個方法,以便你的控制器不包含檢索數據的所有模型邏輯[從控制器中修剪脂肪](http://richardmiller.co.uk/2012/10/31/symfony2-trimming-fat-from-controllers /) –

+0

Adriano,我沒有看到任何protid,但你可以嘗試' - > select('count(c.protid)')'而不是' - > getResult()'我會使用' - > getSingleScalarResult()' – Flask