Symfony2 doctrine form - entity type

I'm trying to add an entity field in a symfony2 form but it's always giving me the same error: '500 (Internal Server Error) '.

This is the class i'm using to create the form. It was automatically programmed with doctrine and CRUD.

class ClientType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('companyName', 'text' , array( 'attr' => array( 'class' => 'companyname' ) ) )
            ->add('contactUserName','text' , array( 'attr' => array( 'class' => 'contactusername' ) ))
            ->add('phone','text' , array( 'attr' => array( 'class' => 'phone' ) ))
            ->add('subdomain','text' , array( 'attr' => array( 'class' => 'subdomain' ) ))
            ->add('email','text' , array( 'attr' => array( 'class' => 'email' ) ))
            ->add('website','text' , array( 'attr' => array( 'class' => 'website' ) ))

         ;

    }

This works fine, but then, i try something like this:

->add('client', 'entity', array(
            'class' => 'BackendBundle:Client'));

'500 (Internal Server Error)'

I tried so many different ways to do this, but it's always the same error. The thing is, i can add or remove the fields that were created at the beggining when this class was done by the doctrine CRUD but if i try adding more fields with different types, it won't let me.

Should i make my own Type class so i can customize my forms or is there a way to modify the form doctrine made?

TY

Upvotes: 0

Views: 469

Answers (1)

Alex
Alex

Reputation: 1573

The thing is, i can add or remove the fields that were created at the beggining when this class was done by the doctrine CRUD but if i try adding more fields with different types, it won't let me.

This is because the command that created your ClientType.php did so, based upon the structure of your BackendBundle\Entity\Client.php file. The form is mapped to the entity that you intend to create. If you want more fields on the form, you will need to add the fields as properties in your BackendBundle\Entity\Client.php, then run:

php bin/console doctrine:generate:entities <VENDOR>/<BUNDLE>/Entity/Client

or if using Symfony 2 < version 2.5

php app/console doctrine:generate:entities <VENDOR>/<BUNDLE>/Entity/Client

To generate the getters and setters for that field, and then

php bin/console doctrine:schema:update --force 

or if using Symfony 2 < version 2.5

php app/console doctrine:schema:update --force        

To add the new field(s) to the database table.

Now you can try to add the field as you were, ensuring that the first argument in the add() method exactly matches how you names your property in the entity.

Upvotes: 1

Related Questions