0

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?

goulashsoup
  • 2,639
  • 2
  • 34
  • 60
  • Are you fetching those entities using a special query? If so: Is the `State` entity fetch-joined? – ccKep Nov 06 '17 at 09:19
  • 1
    Also: [DDC-3448](https://github.com/doctrine/doctrine2/issues/4256) is probably related. – ccKep Nov 06 '17 at 09:22
  • No, the entities get loaded automatically by Symfonys `ParamConverter`... But I found the problem on [github](https://github.com/doctrine/doctrine2/issues/4256): `orderBy` does not work on `EAGER`!. Thanks anyway... (You where faster founding the issue! thx) – goulashsoup Nov 06 '17 at 09:24

1 Answers1

1

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.

goulashsoup
  • 2,639
  • 2
  • 34
  • 60