【问题标题】:Why is a many-to-many relation empty?为什么多对多关系是空的?
【发布时间】:2012-11-07 14:27:13
【问题描述】:

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

所以这是一个多对多的关系。

我按照安全教程对我的用户进行身份验证,它运行良好。 http://symfony.com/doc/current/cookbook/security/entity_provider.html

密码被编码;数据库关系工作正常。

我有一个服务,它处理“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;
}

但是现在,我想获取经过身份验证的当前用户的组,它给了我一个空数组...

// 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) {

            }
        }
    }
}

我希望用户根据他的组访问某些功能...所以不获取这些组对我来说真的很糟糕。 有什么想法吗?

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

【问题讨论】:

    标签: php mysql doctrine-orm many-to-many symfony-2.1


    【解决方案1】:

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

    EAGER 模式(例如:@ManyToOne(targetEntity="\My\Model\User\User", fetch="EAGER"))强制教条2 加载所有数据。

    【讨论】:

    • getGroups() 也返回一个空集合,但我没有强制使用 EAGER 模式。 yml 怎么处理?
    • 也许你可以使用fetch: eagerfetchmode: eager。抱歉,我不使用 yml 语法。我想看看 yml 配置文件。
    • 我已经编辑过了。 fetch: eager 引发错误,而 fetchmode: eager 不会改变任何东西 :(
    猜你喜欢
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2020-07-21
    相关资源
    最近更新 更多