【问题标题】:Symfony2 Query Builder Not Returning Many To One ValueSymfony2 查询生成器不返回多对一值
【发布时间】:2013-10-11 19:19:20
【问题描述】:

我有一个实体,该实体有一列“inventoryLcoation_id”,该列具有多对一关系。出于某种原因,当我使用 createQueryBuilder() 时,它不会在我的 ajax 调用中返回该值,但它会返回其他所有内容:id、名称等。这不是 Symfony2 的问题,而是我缺乏知识的问题:)

这是我的查询生成器代码:

 $qb = $this
 ->createQueryBuilder('p')
 ->select('p.id', 'p.name')
 ->where('p.inventoryLocation = :inventoryId')
 ->andWhere('p.account = :account_id')
 ->setParameter('inventoryId', $value)
 ->setParameter('account_id', $account_id)
 ->orderBy('p.id', 'DESC');
 if($qb->getQuery()->getArrayResult()){
      return $qb->getQuery()->getArrayResult();
 }else{
      return false;
 }

我需要编写什么代码才能使其还包含 inventoryLocation 表中映射到列值“inventoryLocation_id”的值?

非常感谢您帮助我了解 Symfony2 的精彩世界!

【问题讨论】:

    标签: php ajax symfony doctrine-orm query-builder


    【解决方案1】:

    尝试加入:

    $qb->join('p.inventoryLocation','i')
    

    并在选择中指定两个实体

    $qb->select('p','i')
    

    应该是这样的:

    $qb = $this
     ->createQueryBuilder('p')
     ->select('p','i')
     ->join('p.inventoryLocation','i')
     ->where('p.inventoryLocation = :inventoryId') // or ->where('i = :inventoryId')
     ->andWhere('p.account = :account_id')
     ->setParameter('inventoryId', $value)
     ->setParameter('account_id', $account_id)
     ->orderBy('p.id', 'DESC');
    

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2015-07-13
      • 2020-06-24
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多