Symfony2查询错误(同时在Doctrine2下工作)

时间:2011-05-12 23:15:05

标签: php mysql symfony doctrine-orm dql

我正在使用Doctrine2和Symfony2,但Symfony2让我无法完成我在Doctrine2(“独立”版本)中完成的任务。

在Doctrine2中:

我有2个课程,结果,票据本身:

namespace Entity;
/** @Entity @Table(name="tickets") */
class Ticket {
    /**  @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
    private $id;
    /** @Column(type="string", length=100) */
    private $title;
    /** @OneToMany(targetEntity="Outcome", mappedBy="ticket") */
    private $outcomes;
    /* with setters and getters for title and outcomes */
    public function __toString() {
        return  $this->getTitle().' ('.$this->getId().')';
    }
    public function __construct () {
        $this->outcomes=new \Doctrine\Common\Collections\ArrayCollection();
    }
}

namespace Entity;
/** @Entity @Table(name="outcomes") */
class Outcome  {
    /** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
    private $id;
    /** @Column(type="string", length=100) */
    private $text;
    /** @ManyToOne(targetEntity="Ticket", inversedBy="outcomes") @JoinColumn(nullable=false) */
    private $ticket;
    public function __toString() {
        return  $this->getText().' ' .' ('.$this->getId().')';
    }
}

在我的index.php中我使用

$query = $em->createQuery ("SELECT t,o FROM Entity\Ticket t LEFT JOIN t.outcomes o WHERE t.id=1");
$query->useResultCache(true);
$tickets_2 = $query->execute();
$ticket_2 = $tickets_2[0];
$amount=count($ticket_2->getOutcomes());
echo $ticket_2." --ticket\n";
echo $amount." --outcomes\n";

我得到正确的输出

现在在Symfony2中我对实体使用以下定义:

namespace test\DemoBundle\Entity;
/** @orm:Entity @orm:Table(name="tickets") */
class Ticket {
    /**  @orm:Id @orm:Column(type="integer") 
         @orm:GeneratedValue(strategy="AUTO") */
    private $id;
    /** @orm:Column(type="string", length=100) */
    private $title;
    /** @orm:OneToMany(targetEntity="Outcome", mappedBy="response") */
    private $outcomes;
    ...
}
namespace test\DemoBundle\Entity;
/** @orm:Entity @orm:Table(name="outcomes") */
class Outcome  {
    /** @orm:Id @orm:Column(type="integer") @orm:GeneratedValue(strategy="AUTO") */
    private $id;
    /** @orm:Column(type="string", length=100) */
    private $text;
    /** @orm:ManyToOne(targetEntity="Ticket", inversedBy="outcomes") @orm:JoinColumn(nullable=false) */
    private $ticket;
    ....
}

现在,我已经设置了一个带有测试功能的控制器:

namespace test\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use test\DemoBundle\Entity\Ticket as Ticket;
use test\DemoBundle\Entity\Outcome as Outcome;
class TicketController extends Controller { 
    ...
    public function testAction() {
        $em = $this->get('doctrine.orm.entity_manager');  
        $query = $em->createQuery ("SELECT t, o
            FROM test\DemoBundle\Entity\Ticket t  
            LEFT JOIN  t.outcomes o
            WHERE t.id=1");
        $query->useResultCache(true);
        $tickets_2 = $query->execute();
        $ticket_2 = $tickets_2[0];
        $amount=count($ticket_2->getOutcomes());
        $prepared_result=$ticket_2." --ticket ".$amount." --outcomes ";
        $response = new Response($prepared_result);
        return $response; 
    }
}

此时我在尝试执行上一个查询时遇到错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“WHERE t0_.id = 1”附近使用正确的语法

来自webdebugger的

我从PDO获得以下语法:

SELECT t0_.id AS id0,
 t0_.title AS title1,
 o1_.id AS id2,
 o1_.text AS text3,
 o1_.ticket_id AS ticket_id4
FROM tickets t0_ 
LEFT JOIN WHERE t0_.id = 1

所以我想在LEFT JOIN之后我遗失了一些东西,这就是为什么我首先得到错误的原因?

我做错了什么?/我该怎么办?

0 个答案:

没有答案