简体   繁体   中英

Symfony 3 - Doctrine 2 - orderBy on oneToMany relation does not work

I have a very simple oneToMany / manyToOne relation between two entities:

One Workflow holds many 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
  ...

Many States holding one 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

But neither orderBy: ASC nor orderBy: DESC orders the states. I ran php bin/console doctrine:schema:update --force --complete --dump-sql , cleared the cache and restarted apache when changing the option.

I also tried doctrines official way of adding the orderBy option: orderBy: {'stateOrder': 'DESC'} / orderBy: {'stateOrder': 'ASC'} with same results...


Here is the query which ran when rendering a form:

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';

So order by is not part of the query...

Any suggestions why this doesn´t work?

It is a known open issue that orderBy does not work in combination with fetch: EAGER . So either deleting fetch: EAGER or sorting the doctrine collection solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM