我想生成一个用于跟踪MySQL查询的DQL:
SELECT * FROM `folders` AS `t` WHERE `t`.`Library` = @myLib AND AND `t`.`Id` NOT IN (
SELECT DISTINCT(`f`.`Id`) FROM `folders` AS `f` JOIN `folders` AS `ff` ON (`f`.`Position` LIKE CONCAT(`ff`.`Position`, '%')) WHERE `ff`.`Active` = 1 AND `ff`.`Library` = @myLib AND `f`.`Library` = @myLib
)
ORDER BY `t`.`Position` ASC
查询在mySQL中正常工作并返回正确的记录
为了生成DQL,我尝试了以下两个选项:
的 1
$query = $em->createQuery("SELECT F FROM MyBundle:Folders T WHERE T.Library = :libid AND T.id NOT IN (
SELECT DISTINCT(F.id) FROM MyBundle:Folders F JOIN MyBundle:Folders FF WITH F.Position LIKE CONCAT(FF.Position, '%') AND F.Library = :libid AND FF.Library = :libid AND FF.Active = true
) ORDER BY T.Position ASC")
->setParameter('libid', $library);
$result = $query->getResult();
2
$q1 = $this->createQueryBuilder('F')
->select('DISTINCT(F.id)');
$q1->join('\MyBundle\Entity\Folders', 'FF', 'WITH', $q1->expr()->like('F.Position', $q1->expr()->literal('CONCAT(FF.Position, \'%\')')))
->where('FF.Active = true')
->andWhere("FF.Library = '$library'")
->andWhere("F.Library = '$library'");
$q2 = $this->createQueryBuilder('T');
$q2->where('T.Library = :libid')
->andWhere($q2->expr()->notIn('T.id', $q1->getDQL()))
->setParameter('libid', $library)
->orderBy('T.Position', 'ASC');
$result = $q2->getQuery()->getResult();
在我看来,这似乎没问题,但我不知道为什么它会以两种方式产生以下异常:
ContextErrorException: Warning: Illegal offset type in
/var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php line 601
任何帮助将不胜感激。
答案 0 :(得分:1)
似乎没有人对此有答案。我发现了一个临时解决方案如下(我称之为临时解决方案,因为我将我的唯一查询更改为两个单独的查询,而且似乎问题在于学说的核心)。
$qb = $this->createQueryBuilder('F')
->select('DISTINCT(F.id)');
$qb->join('\MyBundle\Entity\Folders', 'FF', 'WITH', 'F.Position LIKE CONCAT(FF.Position, \'%\')')
->where('FF.Active = true')
->andWhere("FF.Library = :library")
->andWhere("F.Library = :library")
->setParameter('library', $library);
$included_folders = $qb->getQuery()->getArrayResult();
$query = $this->createQueryBuilder('F')
->where('F.Active = false')
->andWhere('F.Library = :library')
->setParameter('library', $library)
->orderBy('F.Position', 'ASC');
if (!empty($included_folders)) {
if (count($included_folders) > 1)
{
foreach ($included_folders as $index => $value)
{
if (is_array($value))
{
$included_folders[$index] = !empty($value['id']) ? $value['id'] : $value[1];
}
}
$query->andWhere($query->expr()->notIn('F.id', $included_folders));
}
else {
$query->andWhere('F.id != :folder ')
->setParameter('folder', $included_folders[0]);
}
}
$result = $query->getQuery()->getResult();
正如您所看到的那样,不是从我的第一个查询中获取dql并将其放在notIn
部分中的第二个dql中,这将导致警告消息,我执行第一个查询并获取结果然后将结果放入如果返回值的数量多于一个,则在notIn
内;否则它应该是常规!=
。这解决了我现在的问题,但正如你看到的交易量现在增加了
如果有人有更好的解决方案或任何修复警告,我将感激不尽。
答案 1 :(得分:1)
我遇到了同样的错误,似乎已经在Doctrine / ORM的最新主干中修复了这个错误。
在作为doctrine / orm的composer.json中使用“2.5.*@dev”作为版本应修复此错误,并允许您在单个查询中执行所需操作。