0

我尝试启用 fosuserbundle 的组功能,遵循 fosuserbundle git 上的文档

注册控制器:

//...
// I use the controller action to dump and test
// I created the group
//        $g = new Group('super_admin', array('ROLE_USER', 'ROLE_RESELLER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN'));
//        $g1 = new Group('admin', array('ROLE_USER', 'ROLE_RESELLER', 'ROLE_ADMIN'));
//        $g2 = new Group('reseller', array('ROLE_USER', 'ROLE_RESELLER'));
//        $g3 = new Group('user', array('ROLE_USER'));
//        
//        $em = $this->getDoctrine()->getEntityManager();
//        $em->persist($g);
//        $em->flush();
//        $em->persist($g1);
//        $em->flush();
//        $em->persist($g2);
//        $em->flush();
//        $em->persist($g3);
//        $em->flush();
$em = $this->getDoctrine()->getEntityManager();
$this->getUser()->addGroup($em->getRepository('vRonnUserBundle:Group')->findOneByName('super_admin'));
exit(var_dump($this->getUser()->getGroups()));

捆绑\实体\用户.php

use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

class User extends BaseUser {

//...

/*
* @ORM\ManyToMany(targetEntity="vRonn\UserBundle\Entity\Group", inversedBy="users")
* @ORM\JoinTable(name="fos_user_user_group",
*      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
*      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;

//...
public function __construct() {
    parent::__construct();
    // your own logic

    //$this->groups = new ArrayCollection();
}

bundle\Entity\Group.php

use FOS\UserBundle\Model\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_group")
 */
class Group extends BaseGroup {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="vRonn\UserBundle\Entity\User", mappedBy="groups")
     */
    protected $users;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

我猜需要 fos_user_user_group 表来存储用户组记录,但不存在,我四处搜索并没有创建它的命令,但我找到了结构,所以我在 phpmyadmin 手动创建它

CREATE TABLE IF NOT EXISTS `fos_user_user_group` (
    `user_id` int(11) NOT NULL,
    `group_id` int(11) NOT NULL,
    PRIMARY KEY (`user_id`,`group_id`),
    KEY `IDX_B3C77447A76ED395` (`user_id`),
    KEY `IDX_B3C77447FE54D947` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

最后我认为一切都准备好了,所以我尝试在控制器上转储

$em = $this->getDoctrine()->getEntityManager();
$this->getUser()->addGroup($em->getRepository('vRonnUserBundle:Group')->findOneByName('admin'));
exit(var_dump($this->getUser()->getGroups()));

最后,我的用户在组管理员中???

但是当我尝试以下

//        $em = $this->getDoctrine()->getEntityManager();
//        $this->getUser()->addGroup($em->getRepository('vRonnUserBundle:Group')->findOneByName('admin'));
exit(var_dump($this->getUser()->getGroups()));

我的组是空的,所以我使用 phpmyadmin 检查 db,查看 fos_user_user_group,有 0 行,我想念什么吗?添加组永远不会插入数据库。

i google around and found that can assign group to user when user register successfully using event listener, so i give it a try, but same there is no record inserted

bundle\EventListener\UserCreationListener.php

public function onRegistrationSuccess(FormEvent $event) {
        $this->user = $event->getForm()->getData();
        $group_name = 'user';
        $entity = $this->em->getRepository('vRonnUserBundle:Group')->findOneByName($group_name); // You could do that by Id, too
        $this->user->addGroup($entity);
        $this->em->flush();
    }
4

1 回答 1

0

Finally, i found the solution, i forget to get the usermanager and updateuser

$em = $this->getDoctrine()->getEntityManager();
$this->getUser()->addGroup($em->getRepository('vRonnUserBundle:Group')->find(1));
$userManager = $this->get('fos_user.user_manager');
$userManager->updateUser($this->getUser());
exit(var_dump($this->getUser()->getGroups()->first()));

hope will help someone in someday, more about the usermanager click here

于 2015-01-01T16:47:26.580 回答