symfony - Doctrine 获取一些列和关联实体

标签 symfony doctrine-orm

我试图在这里做一件非常简单的事情,但似乎我还无法获得 DQL。此外,当我在其中输入“DQL”时,谷歌决定让我的日子变得艰难,因为它会返回“SQL”的结果。

我有一个具有各种属性的文章实体。其中之一是关联的图像实体:

/**
 * @ORM\ManyToOne(targetEntity="Image", inversedBy="articles")
 */
protected $image;

现在,为了防止延迟加载和它生成的不必要的加载,我想获取 Article.title 和 Article.image,仅此而已!只需文章名称及其关联图像(整个图像实体)。

我该怎么做?我尝试了我想出的一切,希望某些东西能够真正起作用,并且我能够检查为什么它起作用而其他东西不起作用,但我就是无法让它起作用。

这是我想出的最理智的查询版本,前提是它有效:

public function getTitleBackground($id)
{
    $qb = $this->createQueryBuilder('a')
               ->select('a.title, a.id', 'i')
               ->leftJoin('a.image', 'i')
               ->where('a.id = :id')
               ->setParameter('id', $id);

    return $qb->getQuery()->getResult();
}

这给我返回了这个错误:

An exception has been thrown during the rendering of a template ("[Semantical Error] line 0, col -1 near 'SELECT a.title,': Error: Cannot select entity through identification variables without choosing at least one root entity alias.") in PIFlexBlogBundle:Admin/Page:commentEdit.html.twig at line 13

当我在 ->select() 中添加 a 时,这个问题就“修复”了,但随后它为我选择了所有文章字段,这正是我想要的试图逃避。

注意:我在 symfony2 存储库类中使用它。

最佳答案

关于symfony - Doctrine 获取一些列和关联实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14275158/

相关文章:

php - 我是否在正确的位置定义了 KERNEL_CLASS?

php - Symfony2 : routing with controller as a service not working

php - 奏鸣曲管理员 - sonata_type_collection : select from the list of existing entities

php - 使用串联 ID 在数据库字段上创建学说关联

symfony - Doctrine 2.1 - 多对多级联合并后关系丢失 - Symfony2

PHP 7.2 抛出 run "composer require symfony/dotenv",7.1 没有

php - 教义中的并发

php - Doctrine2/Symfony 2 @ParamConverter 使用自定义存储库方法删除未映射的参数

mysql - 学说 2 : how to use datatype long?

php - 最后的字段在 POST 中丢失