2
votes

I'm following Using Groups With FOSUserBundle Doc Symfony https://symfony.com/doc/current/bundles/FOSUserBundle/groups.html.

And after i'm generating CRUD Group Controller Based on a Doctrine Entity

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

So, I have :

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;
    }

}

Part of 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;
}
[...]
}

Part of GroupController.php (CRUD Symfony generating)

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

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

Part of grouprole.yml

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

When i access to ../web/app_dev.php/grouprole/new to create a new group by the Form from GroupRoleController.php, I get error :

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

Normally, when I created a new entity by CRUD Controller, I don't need to pass any arguments to __construct()!?

Maybe are there an another way to used CRUD with group FOS?

1

1 Answers

0
votes

You have mistakes in your constructor, which are whats causing your issue.

current

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

This is not the way you create a form in Symfony. You must create a formType class, and pass that to $this->createForm(xx) (notice you've typo'd the method call as well).

Once you create a formtype, which might looks something like this:

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
    }
}

then in your controller:

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 will know that the original class has a construct, and use the form to populate it.