加入Doctrine查询语言

时间:2017-08-18 20:40:58

标签: sql-server symfony dql

我在将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();

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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