Flo Schild
Flo Schild

Reputation: 5294

Why is a many-to-many relation empty?

I am building a website with a user authentified part, on Symfony 2.1.3, with Doctrine 2. Each user can be a member of a "usergroup", and each usergroup can contains many users.

So it is a many-to-many relation.

I follow the security tutorial to authentify my users, and it works perfectly. http://symfony.com/doc/current/cookbook/security/entity_provider.html

The passwords are encoded; the database relation works fine.

I have got a service, that handle the "LoginSuccess" event, and add the user to the session. I think it works correctly, because I find my users properties in my parent front controller.

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
    $session = $request->getSession();

    $session->set('_user', $token->getUser());

    if ($session->has('referer')) {
        if (($session->get('referer') !== null) && ($session->get('referer') !== '')) {
            $response = new RedirectResponse($session->get('referer'));
        } else {
            $response = new RedirectResponse($request->getBaseUrl() . '/');
        }
    } else {
        // if no referer then go to homepage
        $response = new RedirectResponse($request->getBaseUrl() . '/');
    }

    return $response;
}

But now, I want to get the groups of the authentified current user, and it give me an empty array...

// Somewhere in a controller action.
$session = $this->getRequest()->getSession();
$current_user = $session->get('_user');
echo '<pre>';
exit(var_dump($current_user));
object(Me\MyProject\CoreBundle\Entity\User)#38 (16) {
    [...]
    ["groups":"Me\MyProject\CoreBundle\Entity\User":private] => object(Doctrine\ORM\PersistentCollection)#34 (9) {
        ["snapshot":"Doctrine\ORM\PersistentCollection":private] => array(0) {

        }
        ["owner":"Doctrine\ORM\PersistentCollection":private] => NULL
        ["association":"Doctrine\ORM\PersistentCollection":private] => NULL
        ["em":"Doctrine\ORM\PersistentCollection":private] => NULL
        ["backRefFieldName":"Doctrine\ORM\PersistentCollection":private] => NULL
        ["typeClass":"Doctrine\ORM\PersistentCollection":private] => NULL
        ["isDirty":"Doctrine\ORM\PersistentCollection":private] => bool(false)
        ["initialized":"Doctrine\ORM\PersistentCollection":private] => bool(false)
        ["coll":"Doctrine\ORM\PersistentCollection":private] => object(Doctrine\Common\Collections\ArrayCollection)#33 (1) {
            ["_elements":"Doctrine\Common\Collections\ArrayCollection":private] => array(0) {

            }
        }
    }
}

I want the user to access some features depending on his group(s)... So It is really bad for me not to get these groups. Any idea ?

My yml model configuration file :

Me\MyProject\CoreBundle\Entity\User:
type: entity
repositoryClass: FSB\Intranet\CoreBundle\Repository\UserRepository
table: users
fields:
    id:
        id: true
        type: integer
        unsigned: false
        nullable: false
        generator:
            strategy: IDENTITY
[...]
manyToMany:
    groups:
        targetEntity: Usergroup
        inversedBy: users
        joinTable:
            name: usergroups_for_user
            joinColumns:
                user_id:
                    referencedColumnName: id
            inverseJoinColumns:
                usergroup_id:
                    referencedColumnName: id
lifecycleCallbacks: {  }

Upvotes: 2

Views: 1332

Answers (1)

jeremieca
jeremieca

Reputation: 1188

Doctrine load the ArrayCollection _elements only when you call the function getGroups because, by default, the EAGER mode is inactive. What is the result of getGroups() function ?

The mode EAGER (Example : @ManyToOne(targetEntity="\My\Model\User\User", fetch="EAGER")) force doctrine2 to load all the data.

Upvotes: 2

Related Questions