在查询生成器Dql

问题描述:

我一直在工作一些dql问题的一些日子。在查询生成器Dql

在我的模型中,y有2个实体(机器和实例)。是一对多的关系,所以一个机器可以有很多实例,一个实例只能有一台机器。

现在,我想要获得所有具有一些实例的机器。在正常DQL我得到它正常工作,这样的:

$ids = array(...) 
$dql = "select M from AppBundle:Machine M INNER JOIN AppBundle:Instancia I WITH M.id = I.machine WHERE I.software IN (:ids)"; 
$query = $em->createQuery($dql)->setParameter('ids', $ids); 
$maquinas = $query->getResult(); 

但是,当我想在QueryBuilder的我没有得到它的工作来翻译。我正在尝试一些方法:

//First way 
$qb = $this->createQueryBuilder("M") 
->innerJoin('Instancia', 'I', 'WITH', 'M.id = I.machine') 
->where('I.software IN (:ids)') 
->setParameters('ids', $ids);  
$maquinas = $qb->getQuery()->getResult(); 

//Second way 
$qb = $this->createQueryBuilder("x") 
->select('M') 
->from('AppBundle:Machine', 'M') 
->innerJoin('Instancia', 'I', 'WITH', 'M.id = I.machine') 
->where('I.software IN (:ids)') 
->setParameters('ids', $ids);  
$maquinas = $qb->getQuery()->getResult(); 

我的过错是什么?

哪种方式更快,简单的dql或querybuilder?

非常感谢!

+0

你得到任何错误? –

+0

我得到这个错误:致命错误:在非对象上调用__clone方法 – Angel

+0

可能有任何可能在dql语句中添加where子句? 我的意思是例如: $ dql =“从AppBundle中选择M:Maquina M INNER JOIN AppBundle:Instancia I WITH M.id = I.maquina”; $ query = $ em-> createQuery($ dql); $ query-> where(“I.software IN(:ids)”); $ query-> setParameter('ids',$ ids); – Angel

使用表达式:

$qb = $this->createQueryBuilder("M") 
->innerJoin('Instancia', 'I', 'WITH', 'M.id = I.machine') 
->where($qb->expr()->in('I.software', $ids)); 
$maquinas = $qb->getQuery()->getResult(); 

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class