我有一个实体,它有一个具有多对一关系的列"inventoryLcoation_id"。出于某种原因,当我使用 createQueryBuilder() 时,它不会在我的 ajax 调用中返回该值,但它返回其他所有内容:id、name 等。这不是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_id"的 inventoryLocation 表中的值?
非常感谢您的帮助,启发了我对Symfony2的精彩世界的理解!
尝试连接:
$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');