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 構文にエラーがあります。1 行目の 'WHERE t0_.id = 1' 付近で使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。
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の後に何かが欠けていると思うので、そもそもエラーが発生するのはなぜですか?
私は何を間違っていますか?/どうすればいいですか?