skip to Main Content

I’ve a Symfony 4 project with User entity and SoldeConges Entity.
An user has a SoldeConges collection.

But when I dump the $user->getSoldeConges(), the collection is empty.

My User entity :

/**
 * @ORMOneToMany(targetEntity="AppEntitySoldeConges", mappedBy="user", orphanRemoval=true)
 */
private $soldeConges;

/**
     * @return Collection|SoldeConges[]
     */
    public function getSoldeConges(): Collection
    {
        return $this->soldeConges;
    }

And my user has 3 soldeConges :

PhpMyAdmin SoldeConge table :

enter image description here

And when I make a dump in my controller for my User (which is the user number 1) :

$soldeConges = $this->getUser()->getSoldeConges();
        dump($soldeConges);

I’ve :

enter image description here

So, why can not access to my User SoldeConges collection ?

2

Answers


  1. 1)To get your soldeConges (this is symfony 3 code, adapt it to 4 😉 ):

    $em = $this->getDoctrine()->getManager();
    $soldeCongesRepository= $em->getRepository('AppSoldeConges:SoldeConges');
    $soldeConges = $soldeCongeRepository->findBy(['userId'=>$this->getUser()->getId()]);
    

    2)It may be due to Doctrine lazy loading.
    Try fetch=”EAGER” (it’s LAZY by default):

     * @ORMOneToMany(targetEntity="AppEntitySoldeConges", mappedBy="user", orphanRemoval=true, fetch="EAGER")
    
    Login or Signup to reply.
  2. Doctrine loads the whole collection in once IF you try to access it. A dump is the memory model at the moment where you place your dump() statement.

    If you should render the collection first (or even only if you use the count() method on the collection) and then use the dump() statement you will see that your collection has been loaded. This is the system called lazy loading. It will execute a second query when needed. But as you may know if two queries could get one query then it should be better and faster.
    On the other hand if you have entities with large collections this could get a serious problem. In that case you could use “extra lazy loading”. (See the docs)

    Anyway if you want to get your collections loaded immediately with your entities then you could use your own DQL query that have one or more JOINS. Below an example of your Repository with a new function called findAllWithJoin. Call that function from your controller instead of findAll().

    namespace AppRepository;
    
    use AppEntityUser;
    use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
    use DoctrineCommonPersistenceManagerRegistry;
    
    class UserRepository extends ServiceEntityRepository
    {
        public function __construct(ManagerRegistry $registry)
        {
            parent::__construct($registry, User::class);
        }
    
        public function findAllWithJoin()
        {
            $entityManager = $this->getEntityManager();
    
            $query = $entityManager->createQuery('SELECT u, sc FROM User u JOIN u.soldeConges sc');
    
            return $query->execute();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search