为什么多对多关系是空的?

时间:2022-04-13 08:03:42

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.

我正在Symfony 2.1.3上使用Doctrine 2构建一个带有用户身份验证部分的网站。每个用户都可以是“用户组”的成员,每个用户组可以包含许多用户。

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

我按照安全教程来验证我的用户身份,它完美无缺。 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.

我有一个处理“LoginSuccess”事件的服务,并将用户添加到会话中。我认为它工作正常,因为我在父前端控制器中找到了我的用户属性。

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 :

我的yml模型配置文件:

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: {  }

1 个解决方案

#1


2  

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 ?

只有在调用函数getGroups时,Doctrine才会加载ArrayCollection _elements,因为默认情况下,EAGER模式处于非活动状态。 getGroups()函数的结果是什么?

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

模式EAGER(例如:@ManyToOne(targetEntity =“\ My \ Model \ User \ User”,fetch =“EAGER”))强制doctrine2加载所有数据。

#1


2  

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 ?

只有在调用函数getGroups时,Doctrine才会加载ArrayCollection _elements,因为默认情况下,EAGER模式处于非活动状态。 getGroups()函数的结果是什么?

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

模式EAGER(例如:@ManyToOne(targetEntity =“\ My \ Model \ User \ User”,fetch =“EAGER”))强制doctrine2加载所有数据。