Saturday, December 28, 2013

SonataAdminBundle - Making fields for two entities (Many to many relation)

If you always got the "Catchable Fatal Error: Object of class Acme\Bundle\DemoBundle\Entity\Roles could not be converted to string.", this tutorial will provide you a way to deal with it.
First of all, the error is returned because the `__toString()` class is not overridden. So, let make it! 
    //Acme\Bundle\DemoBundle\Entity\Roles
    function __toString()
    {
        return $this->getName();
    }
In order to make a checkbox of the roles available, we should specify true values for  `expanded`, `compound`, and `multiple`
   //Acme\Bundle\DemoBundle\Admin\UsersAdmin
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('Roles','sonata_type_model',array(
                  'expanded' => true,
                  'compound' => true,
                  'multiple' => true))

    ;    }

No comments:

Post a Comment