没有ON子句的Doctrine DBAL join

时间:2016-10-08 08:14:19

标签: php mysql symfony doctrine-orm doctrine

在纯SQL中我会这样做

select t1.id, t2.id from table1 t1 join table2 t2;

你会使用doctrine dbal querybuilder

实现同样的目标
$qb = $this->_em->getConnection()->createQueryBuilder()

$qb->select('t1.id, t2.id')
   ->from('table1','t1')
   ->join('t1', 'table2', 't2') //without on clause, this doesn't work
   ->execute()
   ->fetchAll();

2 个答案:

答案 0 :(得分:1)

试试这个

$qb->select('t1.id', 't2.id')
   ->from('table1','t1')
   ->join('t1', 'table2', 't2', true)
   ->execute()
   ->fetchAll();

答案 1 :(得分:1)

没有“ON”表达式的JOIN也称为CROSS JOIN。 根据文档,您无法使用查询构建器

执行此操作

http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/query-builder.html#join-clauses

仅限内,左,右。

你只需要一个原生查询:

$connection = $em->getConnection();

$statement = $connection->prepare("
    select t1.id, t2.id from table1 t1 join table2 t2
");

$statement->execute();

$results = $statement->fetchAll();

或者想更多地了解你想要收到什么