【问题标题】:Join in Doctrine Query Language加入 Doctrine Query Language
【发布时间】:2017-08-18 20:40:57
【问题描述】:

我在将 sql 转换为 dql 时遇到问题。当用户在活动表中时,我试图实现这一点,他们不应该能够编辑非活动状态,但状态位于另一个表中。

SQL

SELECT Activity1.Payrollperiodid, PayrollPeriod.state
FROM Activity1
INNER JOIN PayrollPeriod ON Activity1.payrollperiodid = Payrollperiod.payrollperiodid
where PayrollPeriod.state=1;

DQL

 public function findBytransid($payrollperiodid)
{
  $repository=$this->getEntityManager()->getRepository('comtwclagripayrollBundle:Activity');
  $qb = $repository->createQueryBuilder('a');
  $qb->select('a');
  $qb->innerJoin('a.payrollperiodid', 'p');
  $qb->where('a.payrollperiodid=:payrollperiodid and a.status=:1');
  $qb->setParameter('payrollperiodid', $payrollperiodid);
  $qb->setParameter('status', 1);
  return $results = $qb->getQuery()->getResult();

【问题讨论】:

  • :1 更改为:status

标签: sql-server symfony dql


【解决方案1】:

Same problem with answer

也许这能解决你的问题

$em = $this->getEntityManager();    
$qb =  $em->getRepository('comtwclagripayrollBundle:Activity')
        ->createQueryBuilder('a');

return $qb
    ->select('a')
    ->innerJoin('a.payrollperiodid', 'p',Join::WITH,  $qb->expr()->eq('a.payrollperiodid',':id'))
    ->where($qb->expr()->eq('a.status',':status'))
    ->setParameters([
        'id'=>$payrollperiodid,
        'status' =>1
    ])
    ->getQuery()
    ->getResult();

【讨论】:

【解决方案2】:
$repository=$this->getEntityManager()->getRepository('comtwclagripayrollBundle:Activity');
        $qb = $repository->createQueryBuilder('a');
        $qb->select('a','pp');
        $qb->Join('a.payrollperiodid','pp');
        $qb->where('pp.state = :state');
        $qb->setParameter('state', 0);

        return $results = $qb->getQuery()->getResult();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多