【发布时间】:2017-11-07 07:43:37
【问题描述】:
我在我的 Symfony 项目中使用 Doctrine 时遇到问题。 这是我的代码:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Common\Persistence\ObjectManager;
use Factory\MainBundle\Entity\Lastauction;
use Factory\MainBundle\Entity\Offers;
class AuctionListController extends Controller
{
private $em;
private $qb;
private $offer;
public function getAuctionListAction()
{
$this->em = $this->getDoctrine()->getEntityManager();
$this->qb = $this->em->createQueryBuilder();
getAllAccount();
for($offset=0; $offset<20000; $offset++)
{
$this->insertOffers();
}
return new Response('Offers list generated');
}
private function getAllAccount()
{
$this->qb->select('ac.mail')
->from('FactoryMainBundle:Account', 'ac');
$result = $this->qb->getQuery()->getResult();
$this->accounts = array_column($result, 'mail');
}
private function insertOffers()
{
foreach($this->offers as $id => $offer)
{
if($this->checkIfExsistOffer($offer))
{
echo $offer .'<br>';
$offerObj = new Offers();
$offerObj->setOffer($offer);
$offerObj->setPage(2);
$this->em = $this->getDoctrine()->getManager();
$this->em->persist($offerObj);
if (($id % $this->batchSize) == 0)
{
$this->em->flush();
$this->em->clear();
}
}
}
$this->em->flush();
$this->em->clear();
$this->offers = array();
}
private function checkIfExsistOffer($offerUrl)
{
$this->qb->select('o.id')
->from('FactoryMainBundle:Offers', 'o')
->where("o.offer=:offer")
->setParameter('offer', $offerUrl);
$result = $this->qb->getQuery()->getResult();
return (isset($result['0']['id'])) ? 1 : 0;
}
}
我得到这个错误:
[语义错误] 第 0 行,第 134 列靠近 'o WHERE o.of':错误:'o' 已定义。
[2/2] QueryException: [Semantical Error] line 0, col 134 near 'o WHERE o.of': Error: 'o' is already defined。
[1/2] QueryException: SELECT o.id FROM FactoryMainBundle:Account ac, FactoryMainBundle:Lastauction la, FactoryMainBundle:Offers o, FactoryMainBundle:Offers o WHERE o.offer='123'
为什么 Doctrine 从其他查询中获取表?
【问题讨论】:
-
没有任何上下文(例如,您的类映射和查询构建器的其余部分),我们真的无法告诉您。除了:为什么要在 DQL 中使用变量而不是参数并绑定它们?
-
我更新了我的帖子。我希望现在有足够的代码来分析。
-
您总是使用相同的查询构建器对象,只是将
?select from`-tables 添加到它。 -
是的,这就是答案:) 谢谢
标签: php mysql symfony doctrine-orm