我正在尝试配置 CRUD 组(FOS 用户包)

I'm trying to configure CRUD Group (FOS User Bundle)

我正在关注 通过 FOSUserBundle 使用组 Doc Symfony https://symfony.com/doc/current/bundles/FOSUserBundle/groups.html.

在我生成 CRUD Group Controller Based on a Doctrine Entity

=> $ php app/console generate:doctrine:crud

所以,我有:

GroupRole.php

<?php
// src/BISSAP/UserBundle/Entity/GroupRole.php

namespace BISSAP\UserBundle\Entity;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_group")
 */
class GroupRole extends BaseGroup
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     protected $id;

    /**
     * Get id
     *
     * @return integer 
     */

    public function getId()
    {
        return $this->id;
    }

}

vendor/friendsofsymfony/user-bunde/ModelGroup的一部分。php

abstract class Group implements GroupInterface

{
    protected $id;
    protected $name;
    protected $roles;

public function __construct($name, $roles = array())
{
    $this->name = $name;
    $this->roles = $roles;
}
[...]
}

GroupController.php的一部分(CRUD Symfony 生成)

public function newAction()
{
    $entity = new GroupRole();
    $form   = $this->createCreateForm($entity);

return $this->render('BISSAPUserBundle:GroupRole:new.html.twig', array(
    'entity' => $entity,
    'form'   => $form->createView(),
));
}

部分grouprole.yml

grouprole_new:
    path:     /new
    defaults: { _controller: "BISSAPUserBundle:GroupRole:new" }

当我访问../web/app_dev.php/grouprole/new时通过表单创建一个新组来自 GroupRoleController.php,我得到错误:

Warning: Missing argument 1 for FOS\UserBundle\Model\Group::__construct(), called in /var/www/bodykoncept/src/BISSAP/UserBundle/Controller/GroupRoleController.php on line 81 and defined

通常情况下,当我通过 CRUD 控制器创建一个新实体时,我不需要将任何参数传递给 __construct()!?

也许还有另一种方法可以将 CRUD 与 FOS 组一起使用?

你的构造函数有错误,这就是导致你出现问题的原因。

当前

$entity = new GroupRole();
$form   = $this->createCreateForm($entity);

这不是您在 Symfony 中创建表单的方式。您必须 create a formType class,并将其传递给 $this->createForm(xx)(请注意您也打错了方法调用)。

创建表单类型后,它可能看起来像这样:

AppBundle/Form/GroupRoleType.php

class GroupRoleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType::class);
        // ... plus whatever other fields you want
    }
}

然后在你的控制器中:

AppBundle/Controller/GroupController

$groupRole = new GroupRole('something');
$form = $this->createForm(GroupRoleType::class, $groupRole); 

$form->handleRequest($request);

Its worth nothing here that createForm takes a 2nd param, which defines the initial data in the form. If you pass in your new groupRole object, itll prepopulate the form with the name field, which your user can then change. If you dont want to do this, pass in nothing, and create your new groupRole object after the submit and manually bind the data to it from the form.

Symfony 会知道原来的 class 有一个构造,并使用表单来填充它。