没有ON子句的DBAL连接


Doctrine DBAL join without ON clause

在普通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();

试试这个

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

不带"ON"表达式的JOIN也称为CROSS JOIN。根据文档,你不能使用query builder

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

只有INNER, LEFT, RIGHT.

你只需要一个本地查询:

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

或者想想你真正想要收到什么