Symfony2中的角色问题

时间:2012-03-12 13:34:26

标签: php join symfony doctrine-orm

我正在关注安全性(Link)的symfony2食谱,我的登录表单工作正常,所以我现在尝试管理角色。

我实际上有两个实体:用户和角色,用户有角色列(角色的id)和带有idrole和namerole列的角色。

所以我只想在它们之间建立多对一关系(带连接),以便在用户登录时获取角色的名称。

我尝试在user.orm.xml中添加此代码:

<many-to-one field="role" target-entity="PcastRole">
  <join-columns>
    <join-column name="namerole" referenced-column-name="idrole" />
  </join-columns>
</many-to-one>

但我收到了一个错误:

ORA-00904: "I0_"."IDROLE": invalid identifier

我没有找到转发我的entiere请求以查看问题的方法。

如果您需要,我会将您的登录请求添加到您:

public function loadUserByUsername($login)
{
    $q = $this
        ->createQueryBuilder('u')
        ->where('u.email = :email OR u.login = :login')
        ->setParameter('email', $login)
        ->setParameter('login', $login)
        ->getQuery()
    ;

    try {
        // The Query::getSingleResult() method throws an exception
        // if there is no record matching the criteria.
        $user = $q->getSingleResult();
    } catch (NoResultException $e) {
        throw new UsernameNotFoundException(sprintf('L\'adresse que vous avez saisie n\'est pas enregistrée "%s".', $login), null, 0, $e);
    }
    return $user;
}

提前感谢您的帮助!


我对我的问题做了一些研究,我发现你是正确的jperovic我要建立多对多的关系。

我尝试和食谱一样,但是我收到了这个错误:

ORA-00904: "IM_USER"."ROLEUSER": invalid identifier 

我的加入有问题,但我看不出来。

我的User.orm.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Promocast\UtilisateurBundle\Entity\ImUser" table="IM_USER" repository-class="Promocast\UtilisateurBundle\Entity\ImUserRepository">
    <id name="iduser" type="decimal" column="IDUSER">
      <generator strategy="SEQUENCE"/>
    </id>
    <field name="adresseip" type="string" column="ADRESSEIP" length="25"/>
    <field name="commentaires" type="string" column="COMMENTAIRES" length="4000"/>
    <field name="datecreation" type="date" column="DATECREATION"/>
    <field name="dateDebut" type="date" column="DATE_DEBUT"/>
    <field name="dateExpire" type="date" column="DATE_EXPIRE"/>
    <field name="email" type="string" column="EMAIL" length="100"/>
    <field name="estactif" type="decimal" column="ESTACTIF"/>
    <field name="fonctionutilisateur" type="string" column="FONCTIONUTILISATEUR" length="500"/>
    <field name="idgroupe" type="decimal" column="IDGROUPE"/>
    <field name="idparent" type="integer" column="IDPARENT"/>
    <field name="nomrole" type="string" column="NOMROLE"/>
    <field name="login" type="string" column="LOGIN" length="100"/>
    <field name="mobile" type="string" column="MOBILE" length="25"/>
    <field name="nom" type="string" column="NOM" length="500"/>
    <field name="password" type="string" column="PASSWORD" length="100"/>
    <field name="preferenceadministration" type="string" column="PREFERENCEADMINISTRATION" length="4000"/>
    <field name="prenom" type="string" column="PRENOM" length="500"/>
    <field name="telephone" type="string" column="TELEPHONE" length="25"/>
    <many-to-many field="roleuser" inversed-by="iduser" target-entity="PcastRole">
        <join-table name="IM_USER">
            <join-columns>
                <join-column name="iduser" referenced-column-name="IDUSER"/>
            </join-columns>
            <inverse-join-columns>
                <join-column name="roleuser" referenced-column-name="IDROLE"/>
            </inverse-join-columns>
        </join-table>
    </many-to-many>
  </entity>
</doctrine-mapping>

我的Role.orm.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Promocast\UtilisateurBundle\Entity\PcastRole" table="PCAST_ROLE">
    <id name="idrole" type="integer" column="IDROLE">
      <generator strategy="SEQUENCE"/>
    </id>
    <field name="nomrole" type="string" column="NOMROLE" length="250"/>
    <many-to-many field="iduser" mapped-by="roleuser" target-entity="ImUser" />
  </entity>
</doctrine-mapping>

创建请求:

SELECT t0.NOMROLE AS NOMROLE1, t0.IDROLE AS IDROLE2 FROM PCAST_ROLE t0 INNER JOIN IM_USER ON t0.IDROLE = IM_USER.roleuser WHERE IM_USER.iduser = :param1'

提前感谢!

0 个答案:

没有答案