如何在DELETE QUERY中编写LEFT JOIN?

时间:2011-02-16 22:38:08

标签: php doctrine

您能举例说明使用Doctrine使用左连接的删除查询吗?

2 个答案:

答案 0 :(得分:6)

这是不可能的。见:http://trac.doctrine-project.org/ticket/2142

您必须在where子句中使用子查询:http://www.doctrine-project.org/documentation/manual/1_2/en/dql-doctrine-query-language:subqueries

尝试类似的东西:

$q = Doctrine_Query::create()
    ->delete('TableB b')
    ->where('b.id NOT IN (SELECT b.id FROM TableB b \
          INNER JOIN b.TableA a WHERE b.date > NOW() AND a.channel_id = 10)')
    ->execute();

答案 1 :(得分:0)

没有必要在普通的sql中编写查询,你仍然可以使用QueryBuilder:

$entityManager           = $this->getEntityManager();
$queryBuilder            = $entityManager->createQueryBuilder();
$subQueryCityDataSources = $queryBuilder->getEntityManager()->createQueryBuilder()
    ->select('cds')
    ->from(CityDataSource::class, 'cds')
    ->where('cds.dataSource = :dataSource')
;

$queryBuilder->delete($entityClass, 'd');
$queryBuilder->where($queryBuilder->expr()->in('d.cityDataSource', $subQueryCityDataSources->getDQL()));
$queryBuilder->setParameter('dataSource', $dataSource);

$query = $queryBuilder->getQuery();
$query->execute();

这会生成此查询:

DELETE 
    AppBundle\Entity\ConcreteData\News d 
WHERE 
    d.cityDataSource IN
    (
        SELECT
            cds
        FROM
            AppBundle\Entity\CityDataSource cds
        WHERE
            cds.dataSource = :dataSource
    )

重要提示:您必须在外部查询中附加所有参数。