来自关联实体的Symfony查询字段

时间:2012-11-12 12:04:42

标签: symfony doctrine associations entity

我有两个实体之间有关联:

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的所有行并尝试此操作:

$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();

但是得到一个错误说:

  

[语义错误]第0行,第74行附近'cat ='0'AND':错误:类   Labs \ CatalogBu​​ndle \ Entity \ ProductExtraspecs没有字段或   名为cat的协会

如何访问关联实体的字段? 你能帮帮我吗?

提前谢谢你:)

1 个答案:

答案 0 :(得分: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();
相关问题