Doctrine不会让我选择特定字段

时间:2022-04-22 06:45:52

The symfony framework features an app/console file that can be executed via php to perform some maintenance tasks. It allows users to run DQL queries as well:

symfony框架具有app / console文件,可以通过php执行以执行一些维护任务。它还允许用户运行DQL查询:

# php app/console doctrine:query:dql --hydrate=array \
    'SELECT u.id, u.nameFirst, u.nameLast  FROM DatabaseBundle:User u'  
array
  0 => 
    array
      'id' => string '1' (length=1)
      'nameFirst' => string 'jaroslav' (length=8)
      'nameLast' => string 'rakhmatoullin' (length=13)
  1 => 
    array
      'id' => string '2' (length=1)
      'nameFirst' => string 'Båb Kåre' (length=10)
      'nameLast' => string 'Ytrefoss' (length=8)

Observe that I selected three specific columns. The problem I'm having is that a similar query gives me an error when two tables are joined.

观察我选择了三个特定列。我遇到的问题是,当两个表连接时,类似的查询会给我一个错误。

# php app/console doctrine:query:dql  --hydrate=array \
    'SELECT u.id , r FROM DatabaseBundle:User u JOIN u.roles r'

  [Doctrine\ORM\Query\QueryException]                   
  [Semantical Error] line 0, col -1 near 'SELECT u.id ,': 
     Error: Cannot select entity through identification variables 
     without choosing at least one root entity alias.  

The following returns the whole user joined with his roles:

以下内容返回与其角色一起加入的整个用户:

# php app/console doctrine:query:dql  --hydrate=array \
     'SELECT u, r FROM DatabaseBundle:User u JOIN u.roles r'

Obviously, I'm missing something.

显然,我错过了一些东西。

Any ideas? I would appreciate links to appropriate docs too (on this specific matter).

有任何想法吗?我也很感激与相应文档的链接(关于这个具体问题)。

1 个解决方案

#1


29  

From the documentation on "Partial Object Syntax":

从“部分对象语法”的文档:

By default when you run a DQL query in Doctrine and select only a subset of the fields for a given entity, you do not receive objects back. Instead, you receive only arrays as a flat rectangular result set, similar to how you would if you were just using SQL directly and joining some data.

默认情况下,当您在Doctrine中运行DQL查询并仅为给定实体选择字段的子集时,您不会再接收对象。相反,您只接收数组作为扁平矩形结果集,类似于您直接使用SQL并加入某些数据时的方式。

If you want to select partial objects you can use the partial DQL keyword.

如果要选择部分对象,可以使用部分DQL关键字。

php console doctrine:query:dql --hydrate array \
   'SELECT partial s.{name ,id}, partial c.{name, id } 
    FROM DatabaseBundle:ProductCategories c 
    JOIN c.suppliers s ORDER BY s.name, c.name'

#1


29  

From the documentation on "Partial Object Syntax":

从“部分对象语法”的文档:

By default when you run a DQL query in Doctrine and select only a subset of the fields for a given entity, you do not receive objects back. Instead, you receive only arrays as a flat rectangular result set, similar to how you would if you were just using SQL directly and joining some data.

默认情况下,当您在Doctrine中运行DQL查询并仅为给定实体选择字段的子集时,您不会再接收对象。相反,您只接收数组作为扁平矩形结果集,类似于您直接使用SQL并加入某些数据时的方式。

If you want to select partial objects you can use the partial DQL keyword.

如果要选择部分对象,可以使用部分DQL关键字。

php console doctrine:query:dql --hydrate array \
   'SELECT partial s.{name ,id}, partial c.{name, id } 
    FROM DatabaseBundle:ProductCategories c 
    JOIN c.suppliers s ORDER BY s.name, c.name'