php - 为什么多对多关系为空?

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

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

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

我按照安全教程来验证我的用户,并且效果很好。 http://symfony.com/doc/current/cookbook/security/entity_provider.html

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

我有一个服务,可以处理“LoginSuccess”事件,并将用户添加到 session 中。 我认为它工作正常,因为我在父前端 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;
}

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

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

最佳答案

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

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

关于php - 为什么多对多关系为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457652/

相关文章:

php - 在 getResult 上强制瀑布水化

sql - 原则在连接时包含 null

php - 从 PHP 函数返回内容并同时触发 jQuery 操作的最佳方法是什么?

php - 如何规范化编码名称,如 ks_c_5601-1987 到 CP949?

PHP SQL - 更新搜索结果的数据

php - 数据库中所有表的列表,以及每个表中的行数?

php - 实体公共(public)字段的学说继承

php - 如何在 PHP 4 中使用 SOAP/WSDL?

php - 如何在php中引用mysql配置文件

MySQL 在选择时连接到列