0

我在 Symfony2 中有一个原始查询

$sql = "SELECT * FROM Content
    JOIN ContentLearningAreas ON Content.id = ContentLearningAreas.content_id
    JOIN LearningArea ON ContentLearningAreas.learning_area_id = LearningArea.id
    WHERE ContentLearningAreas.learning_area_id = {$id} AND Content.active = 1";

$stmt = $this->getEntityManager()->getConnection()->prepare($sql);

$stmt->execute();

return $stmt->fetchAll();

这很好用,但我希望将结果转换为内容实体,以便它们的功能仍然有效。

Jake\NameOfBundle\Entity\Content

如何才能做到这一点?

编辑

看来这是使用 PDO 而不是 Doctrine 或 Symfony2。

4

2 回答 2

1

请阅读学说 2 中的本机查询

使用 NativeQuery,您可以执行本机 SELECT SQL 语句并将结果映射到 Doctrine 实体或 Doctrine 支持的任何其他结果格式。

于 2013-07-18T10:19:59.620 回答
1

这行得通;

$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
$items = $stmt->fetchAll(\PDO::FETCH_CLASS, "Jake\NameOfBundle\Entity\Content");
于 2013-07-18T10:45:16.080 回答