原则2:从实体及其连接实体中选择特定列

时间:2015-10-03 11:57:41

标签: symfony doctrine-orm doctrine silex

我有这种情况,我必须选择实体的特定列并加入实体。

This statement works and fetches all columns of all Entities.
$this->createQueryBuilder('e')
        ->select('e','bs','t')
        ->leftJoin('e.bits', 'bs')
        ->leftJoin('bs.tests', 't')
        ->getQuery()
        ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)

问题1

我想要这样的东西

尝试1

$this->createQueryBuilder('e')
        ->select('e.title','bs.name','bs.content','t.id','t.date')
        ->leftJoin('e.bits', 'bs')
        ->leftJoin('bs.tests', 't')
        ->getQuery()
        ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)

但这会引发错误,如

[Semantical Error] line 0, col -1 near 'SELECT e.title,': Error: Cannot select entity through identification variables without choosing at least one root entity alias.

尝试2

$this->createQueryBuilder()
        ->select('e.title','bs.name','bs.content','t.id','t.date')
        ->from($this->_entityName, "e")
        ->leftJoin('e.bits', 'bs')
        ->leftJoin('bs.tests', 't')
        ->getQuery()
        ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)

这会引发错误     警告:缺少Doctrine \ ORM \ EntityRepository :: createQueryBuilder()的参数1,

尝试3

This works but only if I select from just 1 Entity 
    $this->createQueryBuilder('e')
        ->select('e.title')
        ->leftJoin('e.bits', 'bs')
        ->leftJoin('bs.tests', 't')
        ->getQuery()
        ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)

问题2     是否有一种智能方法可以从选择查询中删除特定列,而不是编写所有列。我在一个实体中有大约30个列,并且在查询中编写了我想要的27个列非常麻烦

由于

1 个答案:

答案 0 :(得分:0)

尝试像这样使用partial

$this->createQueryBuilder()
    ->select(['partial e.{title}','partial bs.{name, content}','partial t.{id, date}'])
    ->from($this->_entityName, "e")
    ->leftJoin('e.bits', 'bs')
    ->leftJoin('bs.tests', 't')
    ->getQuery()
    ->getArrayResult