0

I have a user table, a module table, and a user_module binding table. The user_module table has user_id and module_id as foreign keys, and an is_activated attribute. When I fetch a row from the slugs of the user and module tables, I can see all the attributes of the user table but nothing on the module table side. It's like it only has one row with null values. Do you know where the problem could come from? Thanks

PartnerController.php on line 121:
App\Entity\UserModule {#678 ▼
  -is_activated: true
  -user: App\Entity\User {#653 ▼
    -id: 3
    -email: "sebastien.74@gmail.com"
    -roles: array:1 [▶]
    -password: "$2y$13$5Cum2J2ldIXDMK2fJRzdKu9MT6T6N/n7jtmyZQrv4lp3q2.y14Xjy"
    -name: "Sébastien"
    -address: "541 route des Mouilles"
    -zipcode: "74160"
    -city: "NEYDENS"
    -isActivated: true
    -partner: null
    -users: Doctrine\ORM\PersistentCollection {#659 ▶}
    -userModules: Doctrine\ORM\PersistentCollection {#661 ▶}
    -isVerified: false
    -created_at: DateTimeImmutable @1660024493 {#654 ▼
      date: 2022-08-09 05:54:53.0 UTC (+00:00)
    }
    +slug: "sebastiene"
  }
  -module: Proxies\__CG__\App\Entity\Module {#698 ▼
    -id: 1
    -name: null
    -slugModule: null
    +__isInitialized__: false
     …2
  }
}
  • In the *UserModule* entity, try adding `fetch="EAGER"` to the annotation of the *Module* relation property.. – Bossman Aug 16 '22 at 14:54

1 Answers1

0

By default Doctrine loads associations only when needed. If you change this dump dump($userModule); to dump($userModule->getModule->getName(); you should be able to see the module name. If you want to fetch everything by default, you have to tell Doctrine that. You can configure that behaviour like following:

<?php

...

/**
 * @Entity
 */
class UserModule
{
    ...

    /**
     * @ManyToMany(targetEntity="Module", fetch="EAGER")
     */
    public $modules;

    ...
}

Note the fetch="EAGER".

Holicz
  • 480
  • 5
  • 16