我遇到了一个有趣的问题,现在我对"为什么"感到好奇。也许这个Doctrine2相关,也许它不是,也许只是OOP。
查看以下Doctrine Repository功能:
public function generateOrderPush(int $quote_id): array
{
$group_quote_id = $this->getEntityManager()->getRepository('QuoteBundle:Quote')->find($quote_id)->getGroupQuoteId();
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$header = $qb->select(['q.quoteId','q.othersQuoteId','IDENTITY(q.distributor)'])
->from('QuoteBundle:Quote', 'q')
->where('q.quoteType =:quote_type')
->andWhere('q.groupQuoteId =:group_quote_id')
->setParameter('quote_type', 'current')
->setParameter('group_quote_id', $group_quote_id)
->setMaxResults(1)
->orderBy('q.quoteId', 'ASC')
->getQuery()
->getArrayResult();
$details = $qb->select(['qt.quoteId', 'qt.othersQuoteId', 'IDENTITY(qt.customer)'])
->from('QuoteBundle:Quote', 'qt')
->join('QuoteBundle:QuoteDetail', 'qd', 'WITH', 'qt.quoteId = qd.quoteId')
->where('qt.quoteType =:quote_type')
->andWhere('qt.groupQuoteId =:group_quote_id')
->setParameter('quote_type', 'current')
->setParameter('group_quote_id', $group_quote_id)
->groupBy('qd.renewalPartNumber')
->orderBy('qt.quoteId', 'ASC')
->getQuery()
->getArrayResult();
return [];
}
第一个查询$header
正确执行,我收回了数据。第二个查询$details
失败,因为错误如下所示:
SELECT q0_.quote_id AS quote_id_0,q0_.QuoteID AS QuoteID_1, q0_.CustomerSiteID AS sclr_2,FROM quote q1_ INNER JOIN quote_detail q2_ ON(q0_.quote_id = q2_.quote_id),引用q0_ WHERE q0_.quote_type = ? AND q0_.group_quote_id =? GROUP BY q2_.RenewalPartNumber ORDER BY q0_.quote_id ASC LIMIT 1'用params [" current",1428]:
SQLSTATE [42S22]:未找到列:1054未知列' q0_.quote_id'在 '关于条款'
我认为Doctrine不知怎么保持我认为我指的是第一个对象(来自之前的)查询,因为我没有为第二个查询qt
添加新的别名。
问题的解决方案可以分为两个不同的功能,但在进入那个功能之前,我想知道为什么会发生这种情况,以及是否有其他方法可以解决问题。
答案 0 :(得分:0)
使用不同的queryBuilder对象:
public function generateOrderPush(int $quote_id): array
{
$group_quote_id = $this->getEntityManager()->getRepository('QuoteBundle:Quote')->find($quote_id)->getGroupQuoteId();
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$anotherQb = $em->createQueryBuilder();
$header = $qb->select(['q.quoteId','q.othersQuoteId','IDENTITY(q.distributor)'])
->from('QuoteBundle:Quote', 'q')
->where('q.quoteType =:quote_type')
->andWhere('q.groupQuoteId =:group_quote_id')
->setParameter('quote_type', 'current')
->setParameter('group_quote_id', $group_quote_id)
->setMaxResults(1)
->orderBy('q.quoteId', 'ASC')
->getQuery()
->getArrayResult();
$details = $anotherQb->select(['qt.quoteId', 'qt.othersQuoteId', 'IDENTITY(qt.customer)'])
->from('QuoteBundle:Quote', 'qt')
->join('QuoteBundle:QuoteDetail', 'qd', 'WITH', 'qt.quoteId = qd.quoteId')
->where('qt.quoteType =:quote_type')
->andWhere('qt.groupQuoteId =:group_quote_id')
->setParameter('quote_type', 'current')
->setParameter('group_quote_id', $group_quote_id)
->groupBy('qd.renewalPartNumber')
->orderBy('qt.quoteId', 'ASC')
->getQuery()
->getArrayResult();
return [];
}