简体   繁体   English

将3表与学说2查询构建器连接

[英]Join 3 table with doctrine 2 query builder

Hey guys i have three table 嘿,我有三张桌子

1) expert_class
2) expert_location
3) expert

i want to fetch data form all three table i know query in mysql ie 我想从所有三个表中获取数据,我知道在mysql中查询,即

select * from expert_class class 
join expert_location loc 
on loc.expert_id = class.expert_id 
join expert e 
on class.expert_id = e.id 
where e.is_delete=0

using this query i got all data whatever i want but the problem is i have to write this query using doctrine query bulider i tried like this 使用此查询,我可以获取所有数据,但我要使用的是学问查询bulider,我必须这样尝试才能编写此查询

$classes = $this->qb
                    ->add('select', 'exp_cls,exp_loc.id as location_id')
                    ->from('Entity\expert_class','exp_cls')
                    ->join('Entity\expert_location', 'exp_loc')
                    ->join('Entity\expert', 'exp')
                    ->where('exp_cls.expert_id = exp.id')
                    ->AndWhere('exp_cls.expert_id = exp_loc.expert_id')
                    ->AndWhere('exp.is_delete = 0')
                    ->getQuery()
                    ->getArrayResult(); 

when i try to run this query i got this Fatal error 当我尝试运行此查询时,出现此致命错误

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'SELECT exp_cls,exp_loc.id as location_id FROM Entity\expert_class exp_cls INNER JOIN Entity\expert_location exp_loc INNER JOIN Entity\expert exp WHERE exp_cls.expert_id = exp.id AND exp_cls.expert_id = exp_loc.expert_id AND exp.is_delete = 0' in C:\xampp\htdocs\projectname\application\libraries\Doctrine\ORM\Query\QueryException.php:39 Stack trace: #0 C:\xampp\htdocs\projectname\application\libraries\Doctrine\ORM\Query\Parser.php(396): Doctrine\ORM\Query\QueryException::dqlError('SELECT exp_cls,...') #1 C:\xampp\htdocs\projectname\application\libraries\Doctrine\ORM\Query\Parser.php(2363): Doctrine\ORM\Query\Parser->syntaxError('Literal') #2 C:\xampp\htdocs\projectname\application\libraries\Doctrine\ORM\Query\Parser.php(2550): Doctrine\ORM\Query\Parser->Literal() #3 C:\xampp\htdocs\projectname\application\libraries\Doctrine\ORM\Query\Parser.php(2485): Doctrine\ORM\Query\Parser-> in C:\xampp\htdocs\projectname\application\libraries\Doctrine\ORM\Query\QueryException.php on line 44

i also looked up this link Doctrine query builder using inner join with conditions but its not helped 我还使用带有条件的内部联接查询了该链接Doctrine查询生成器,但没有帮助

Have you defined relations in your entities? 您是否在实体中定义了关系? Eg: 例如:

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="expert_location", mappedBy="locations")
 */
private $location;

If you did, then you have to use those connections in your join, eg: 如果这样做,则必须在联接中使用这些连接,例如:

->join('exp_cls.locations', 'exp_loc')  // This joins the expert_location entity

You you didn't, then you should add an ON condition: 您没有,那么您应该添加一个ON条件:

use Doctrine\ORM\Query\Expr\Join;

...

->join('Entity\expert_location', 'exp_loc', Join::ON, $qb->expr()->eq('exp_loc.expert_id', 'exp_cls.expert_id '))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM