Pregunta

I have problem with my User Entity. I have code generated by Doctrine it is below:

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Frontend\UserBundle\Entity\SfGuardPermission", inversedBy="user")
     * @ORM\JoinTable(name="sf_guard_user_permission",
     *   joinColumns={
     *     @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="permission_id", referencedColumnName="id")
     *   }
     * )
     */
    protected $permission;

Problem is with join because I can't join user and permission. What I must to do? I must join sf_guard_user with sf_guard_user_group with sf_guard_grop with sf_guard_group_permission with sf_guard_permission. Because I need to get User permission. I do not no how to write join like this in code above. Is it possible?

¿Fue útil?

Solución 2

SBH thx for replay, of course you have right with everything what you have written. But my sf_guard_user_permisson is empty so I can't use it. I can generate this table, this is no problem, but then I will must maintain it. This is next work for me so i wrote code below:

 namespace Frontend\UserBundle\Entity;

 // ...

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     */
    protected $permissions;

    /**
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPermissions()
    {
        $groups = $this->getSfGuardGroups();

        foreach ($groups as $group)
        {
            $groupPermisions = $group->getPermission();
            foreach ($groupPermisions as $groupPermision)
            {
                if (!in_array($groupPermision, $this->permissions)) {
                    $this->permissions[] = $groupPermision;
                }
            }
        }

        return $this->permissions;
    }

    /**
     * @param string $permissionName
     * @return boolean
     */
    public function hasPermission($permissionName)
    {
        $this->getPermissions();
        foreach ($this->permissions as $permission)
        {
            if($permission->getName() === $permissionName) {
                return true;
            }
        }
        return false;
    }

// ..

What do you think about it? Your opinion is very important for me.

Edit: Thx for SBH help, I have got answer for my question. I have hope it will help other people. If you do not understand something please look at SBH answer.

Otros consejos

You can not write this join in one annotation. In fact you gone have three entity tables sf_guard_user, sf_guard_group and sf_guard_permission and two cross tables which you can write as you already started, sf_guard_user_group and sf_guard_group_permission.

But since it looks like you try to migrate some symfony 1.x stuff to symfony 2.x: The sf_guard_user_permisson table in symfony 1.x is a cross table between users and permission, containing extraordinaire permission for a user which are not granted through the groups the user is in, so you are already done.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top