duancan1900 2017-12-05 19:19
浏览 21

用于管理员的Symfony3 fosuserbundle表单

i use Symfony3.3 and want to make a form for the admin administration. The user should be have a group and the group sould have the roles for the backend access. The form for groups (name and roles) i finished and the form for the admins (name, passwort...) is finish too. The admin will be find and have the group. If i load the admin it have the arraycollection with the groups.

Here my classes admin:

class Admin extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(name="idAdmin", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, options={"default":NULL})
     */
    protected $style;

    /**
     * @ORM\ManyToMany(targetEntity="AdminBundle\Entity\AdminGroup")
     * @ORM\JoinTable(
     *     name="admin_has_group",
     *     joinColumns={
     *         @ORM\JoinColumn(name="idAdmin", referencedColumnName="idAdmin")
     *     },
     *     inverseJoinColumns={
     *         @ORM\JoinColumn(name="idGroup", referencedColumnName="idGroup")
     *     }
     * )
     */
    protected $groups;

    /**
     * @return string
     */
    public function getStyle()
    {
        return $this->style;
    }

    /**
     * @param string $style
     */
    public function setStyle($style)
    {
        $this->style = $style;

        return $this;
    }

    public function setGroups($groups)
    {

        $this->groups = $groups;

        return $this;
    }

    public function getGroups()
    {
        return $this->groups;
    }
}

groups

class AdminGroup extends BaseGroup
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(name="idGroup", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * Group constructor.
     *
     * @param string $name
     * @param array  $roles
     */
    public function __construct($name = '', $roles = array())
    {
        parent::__construct($name, $roles);
    }
}

form generation

$admin = $this->getDoctrine()->getRepository(Admin::class)->find(1);
$admingroupList = $this->getDoctrine()->getRepository(AdminGroup::class)->findAll();
$form  = $this->createFormBuilder($admin)
    ->add("username", TextType::class)
    ->add('plainPassword', PasswordType::class, $passwordSettings)
    ->add(
        'groups', ChoiceType::class, [
        'required' => false,
        'multiple' => true,
        'choices'  => $admingroupList,
    ])
    ->add('save', SubmitType::class, ['label' => 'Save'])->getForm()->createView();

save form

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

    $user = $form->getData();

    $em->persist($user);
    $em->flush();

    ....
}

The first problem is that i have in the overview only display the id's in the select. The second problem is that i submit the form (with selected groups) symfony crashed with the following message

Call to a member function contains() on array

I try to convert the grouplist to an normal array they will be crashed at the save the data

Expected argument of type "FOS\UserBundle\Model\GroupInterface", "integer" given

I dont know that i sould do to make a simple symfony form with the admin data and group selection... i dont find any example for a form with fosuserbundle...

Have someone an idea what i can to without manipulate the fosuserbundle entites or the symfonycode?

If you need more source, tell me with part :)


Editing 10.12.17 I try to convert the ChoosenArray into this format

$list = [
'user' => 0,
'admin' => 1
];

but than it will be broken at

$form->handleRequest($request);

with the error:

Expected argument of type "FOS\UserBundle\Model\GroupInterface", "integer" given

  • 写回答

1条回答 默认 最新

  • dqsp60748 2017-12-05 19:31
    关注

    I do not think the data returned from

     $admingroupList = $this->getDoctrine()->getRepository(AdminGroup::class)->findAll();
    

    Will work as you want it too. choices wants something like this

    'choices' => [
        'Admin' => 'admin',
        'User' => 'user'
    ]
    

    Where the key of the array is the name the user sees, and the value of the array is the value used in the <option>.

    You probably need to manipulate the $admingroupList array to mimic the demo array above. Or write your own query in the AdminGroup Repo to return a pre-formatted array for use with a Symfony form.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法