Symfony 3 - Doctrine 2 - orderTy on oneToMany关系不起作用

时间:2017-11-06 08:36:12

标签: php symfony doctrine-orm orm mapping

我在两个实体之间有一个非常简单的oneToMany / manyToOne关系:

一个Workflow拥有许多States

MyBundle\Entity\Workflow:
  type: entity
  id:
    id:
      type: integer
      generator: {strategy: AUTO}
  oneToMany:
    states:
      targetEntity: MyBundle\Entity\State
      mappedBy: workflow
      orderBy:
        stateOrder: ASC
      cascade: [all]
      orphanRemoval: true
      fetch: EAGER
  ...

许多States持有一个Workflow

MyBundle\Entity\State:
  type: entity
  id:
    id:
      type: integer
      generator: {strategy: AUTO}
  manyToOne:
    workflow:
      targetEntity: MyBundle\Entity\Workflow
      inversedBy: states
      cascade: [persist, merge, refresh]
  ...
  fields:
    stateOrder:
      type: smallint

orderBy: ASCorderBy: DESC都没有命令州。我运行了php bin/console doctrine:schema:update --force --complete --dump-sql,清除了缓存并在更改选项时重新启动了apache。

我还尝试doctrines official way添加orderBy选项:orderBy: {'stateOrder': 'DESC'} / orderBy: {'stateOrder': 'ASC'},结果相同......

以下是渲染表单时运行的查询:

SELECT 
  t0.id AS id_1, 
  t0.name AS name_2, 
  t0.project_id AS project_id_3, 
  t4.id AS id_5, 
  t4.stateOrder AS stateOrder_6, 
  t4.workflow_id AS workflow_id_7, 
  t8.id AS id_9, 
  t8.keyWord AS keyWord_10, 
  t8.action AS action_11, 
  t8.workflow_id AS workflow_id_12, 
  t8.from_id AS from_id_13, 
  t8.to_id AS to_id_14, 
  t0.initialState_id AS initialState_id_15 
FROM 
  workflow_Workflow t0 
  LEFT JOIN workflow_State t4 ON t4.workflow_id = t0.id 
  LEFT JOIN workflow_Transition t8 ON t8.workflow_id = t0.id 
WHERE 
  t0.id = '10';

因此,order by不是查询的一部分......

为什么这不起作用的任何建议?

1 个答案:

答案 0 :(得分:0)

已知open issueorderByfetch: EAGER无法合作。因此,删除fetch: EAGERsorting the doctrine collection可以解决问题。