-1

I'm trying to use partials on an entity which contains a ManyToOne or OneToMany relation.

My DQL looks like this:

SELECT partial a.{created, updated} , partial b.{id, value, entity} FROM App\Entity\DataSetting a INNER JOIN a.fileEntityValues b

The problem is that "partial b" has a property called "entity" which is another field with relation defined like this.

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\FileEntity")
 * @ORM\JoinColumn(name="file_entity_id", referencedColumnName="id")
 */
private $entity;

But when i execute the previous query I don't get errors but the "entity" field is just not displayed in the result:

[0]=>
  array(9) {
    ["id"]=>
    int(36)
    ["created"]=>
    object(DateTime)#3022 (3) {
      ["date"]=>
      string(26) "2020-12-31 00:00:00.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["updated"]=>
    object(DateTime)#3021 (3) {
      ["date"]=>
      string(26) "2020-12-31 00:00:00.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["fileEntityValues"]=>
    array(4) {
      [0]=>
      array(2) {
        ["id"]=>
        int(72)
        ["value"]=>
        string(4) "1112"
       // WHY entity field not displayed
      }
      [1]=>
      array(2) {
        ["id"]=>
        int(73)
        ["value"]=>
        string(4) "1111"
      }

WHY ?

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
  • assumption: probably because the many-to-one is usually lazily loaded if not specified to be explicitly loaded. – Jakumi Jan 06 '21 at 22:47
  • 1. The question of how you show the results is absolutely valid: I you use `getResult()` on the query builder you should NOT get an array only if you use `getArrayResult()` or serialization or another special "dumping" form which can influence the result! 2. Since `$entity` is literally an entity, so if DQL is tranformed to SQL `b.entity` will just be an ID since the table of the entity which holds `$entity` only contains the ID in the `file_entity_id` column, so it might help to `JOIN b.entity`. – goulashsoup May 30 '21 at 17:24
  • Tipp: Since you are using Symfony the `Doctrine` tab in the Symfony web panel gives a detailed list of all run SQL queries. From viewing the plain SQL it often is more clear why a DQL doesn't yield expected results. – goulashsoup May 30 '21 at 17:47

1 Answers1

-1

@claudio how did you print the debug output?

see, if you used dump(), or var_dump(), fileEntityValues.entities should be an ArrayCollection()

I guess you dumped an object serialization, excluding in some way fileEntityValues.entity

can you:

  • paste how you printed the debug
  • paste both entities, or at least the constructors
  • paste the result of dump($entity)

hint: don't use partials, they are in some way discouraged also by Doctrine and can lead to incoherent behaviors