0

started to work with symfony2. I like it so far. I tried myself on a multi entity form to figure out, if the form builder is usable for my needs. So first of all my entities:

Customer entity

    namespace CustomerBundle\Entity;

    use Doctrine\Common\Collections\ArrayCollection;
        use Symfony\Component\DependencyInjection\ContainerInterface;
        use Doctrine\ORM\Mapping as ORM;
        use UserBundle\Entity\Users;

        /**
         * @ORM\Table(name="customers")
         * @ORM\HasLifecycleCallbacks
         * @ORM\Entity(repositoryClass="CustomerBundle\Entity\CustomerRepository")
         */
        class Customer
        {
            /**
             * @var ContainerInterface
             */
            protected $container;
            /**
             * @ORM\Id
             * @ORM\Column(name="id", type="integer", nullable=false)
             * @ORM\GeneratedValue(strategy="IDENTITY")
             */
            protected $Id;
            /**
             * @ORM\Column(name="nickname", type="string", length=50, unique=true)
             */
            protected $NickName;
            /**
             * @ORM\OneToMany(targetEntity="\CustomerBundle\Entity\Address", mappedBy="Customer")
             **/
            protected $Addresses;

            /**
             * @ORM\OneToMany(targetEntity="\CustomerBundle\Entity\Mail", mappedBy="Customer")
             **/
            protected $Emails;
            /**
             * @ORM\ManyToMany(targetEntity="\UserBundle\Entity\Users")
             * @ORM\JoinTable(name="customers_mapping_users",
             *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
             *      inverseJoinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="id")}
             *      )
             **/
            protected $Users;
            /**
             * @ORM\Column(name="lead_type", type="string", length=50, nullable=true)
             */
            protected $LeadType;
            /**
             * @ORM\ManyToOne(targetEntity="\UserBundle\Entity\Users", fetch="EXTRA_LAZY")
             * @ORM\JoinColumn(name="lead__owner_user_id", referencedColumnName="id")
             */
            protected $LeadOwner;
            /**
             * @ORM\OneToMany(targetEntity="\CustomerBundle\Entity\Note", mappedBy="Customer")
             **/
            protected $Notes;
            protected $Reminders;
            protected $SupportTickets;
            protected $Invoices;
            protected $Documents;
            /**
             * @ORM\OneToMany(targetEntity="\CustomerBundle\Entity\Todo", mappedBy="Customer")
             **/
            protected $Todos;

            /**
             * @return Todo[]
             */
            public function getTodos()
            {
                return $this->Todos;
            }

            /**
             * @param Todo $todo
             */
            public function addTodo(Todo $todo)
            {
                $this->Todos->add($todo);
            }

            /**
             * @param Todo $todo
             *
             * @return bool
             */
            public function hasTodo(Todo $todo=null)
            {
                return $this->Todos->contains($todo);
            }

            /**
             * @param Todo $todo
             *
             * @return $this
             */
            public function removeTodo(Todo $todo)
            {
                $this->Todos->removeElement($todo);

                return $this;
            }
}

My Address Entity

<?php

    namespace CustomerBundle\Entity;
        use Symfony\Component\DependencyInjection\ContainerInterface;
        use Doctrine\ORM\Mapping as ORM;

        /**
         * @ORM\Table(name="customers_address")
         * @ORM\HasLifecycleCallbacks
         * @ORM\Entity(repositoryClass="CustomerBundle\Entity\AddressRepository")
         */
        class Address
        {
            /**
             * @var ContainerInterface
             */
            protected $container;
            /**
             * @ORM\Id
             * @ORM\Column(name="id", type="integer", nullable=false)
             * @ORM\GeneratedValue(strategy="IDENTITY")
             */
            protected $Id;
            /**
             * @ORM\ManyToOne(targetEntity="CustomerBundle\Entity\Customer", inversedBy="Addresses")
             * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
             **/
            protected $Customer;
            /**
             * @ORM\Column(name="first_name", type="string", length=50, nullable=true)
             */
            protected $FirstName;
            /**
             * @ORM\Column(name="last_name", type="string", length=50, nullable=true)
             */
            protected $LastName;
            /**
             * @ORM\Column(name="company_name", type="string", length=50, nullable=true)
             */
            protected $CompanyName;
            /**
             * @ORM\Column(name="street1", type="string", length=250, nullable=true)
             */
            protected $Street1;
            /**
             * @ORM\Column(name="street2", type="string", length=250, nullable=true)
             */
            protected $Street2;
            /**
             * @ORM\Column(name="street3", type="string", length=250, nullable=true)
             */
            protected $Street3;
            /**
             * @ORM\Column(name="zipcode", type="string", length=10, nullable=true)
             */
            protected $Zipcode;
            /**
             * @ORM\Column(name="city", type="string", length=50, nullable=true)
             */
            protected $City;
            /**
             * @ORM\Column(name="country", type="string", length=50, nullable=true)
             */
            protected $Country;
            /**
             * @ORM\Column(name="phone", type="string", length=50, nullable=true)
             */
            protected $Phone;
            /**
             * @ORM\Column(name="fax", type="string", length=50, nullable=true)
             */
            protected $Fax;
            /**
             * @ORM\Column(name="tax_id", type="string", length=50, nullable=true)
             */
            protected $TaxId;
}

and last but not least

    namespace CustomerBundle\Entity;

    use HomeBundle\Traits\CreateTrait;
        use HomeBundle\Traits\serializeTrait;
        use HomeBundle\Traits\UpdateTrait;
        use Symfony\Component\DependencyInjection\ContainerInterface;
        use Doctrine\ORM\Mapping as ORM;

        /**
         * @ORM\Table(name="customer_mail")
         * @ORM\HasLifecycleCallbacks
         * @ORM\Entity(repositoryClass="CustomerBundle\Entity\MailRepository")
         */
        class Mail
        {
            /**
             * @var ContainerInterface
             */
            protected $container;
            /**
             * @ORM\Id
             * @ORM\Column(name="id", type="integer", nullable=false)
             * @ORM\GeneratedValue(strategy="IDENTITY")
             */
            protected $Id;
            /**
             * @ORM\Column(name="email", type="string", length=255, unique=true)
             */
            protected $Email;
            /**
             * @ORM\ManyToOne(targetEntity="CustomerBundle\Entity\Customer", inversedBy="Emails")
             * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
             **/
            protected $Customer;
}

According to http://symfony.com/doc/current/cookbook/form/form_collections.html I added three Form Types:

Address:

        use Symfony\Component\Form\AbstractType;
        use Symfony\Component\Form\FormBuilderInterface;
        use Symfony\Component\OptionsResolver\OptionsResolver;

        class AddressType extends AbstractType
        {
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder
                    ->add('FirstName','text',array('label'=>'firstname'))
                    ->add('LastName','text',array('label'=>'lastname'))
                    ->add('CompanyName','text',array('label'=>'company name'))
                    ->add('Street1','text',array('label'=>'street 1'))
                    ->add('Street2','text',array('label'=>'street 2'))
                    ->add('Street3','text',array('label'=>'street 3'))
                    ->add('Zipcode','text',array('label'=>'zip code'))
                    ->add('City','text',array('label'=>'city'))
                    ->add('Country','text',array('label'=>'country'))
                    ->add('Phone','text',array('label'=>'phone'))
                    ->add('Fax','text',array('label'=>'fax'))
                    ->add('TaxId','text',array('label'=>'taxid'));
            }
            public function configureOptions(OptionsResolver $resolver)
            {
                $resolver->setDefaults(
                    array(
                    'data_class' => 'CustomerBundle\Entity\Address',
                    )
                );
            }
            public function getName()
            {
                return 'address';
            }
        }

Mail:

        use Symfony\Component\Form\AbstractType;
        use Symfony\Component\Form\FormBuilderInterface;
        use Symfony\Component\OptionsResolver\OptionsResolver;

        class MailType extends AbstractType
        {
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder
                    ->add('Email','email',array('label'=>'email'));
            }
            public function configureOptions(OptionsResolver $resolver)
            {
                $resolver->setDefaults(
                    array(
                    'data_class' => 'CustomerBundle\Entity\Mail',
                    )
                );
            }
            public function getName()
            {
                return 'mail';
            }
        }

and the customer as top level instance:

    <?php
        namespace CustomerBundle\Form\Type;

        use Symfony\Component\Form\AbstractType;
        use Symfony\Component\Form\FormBuilderInterface;
        use Symfony\Component\OptionsResolver\OptionsResolver;

        class CustomerType extends AbstractType
        {
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder
                    ->add('NickName','text',array('label'=>'customers nickname'))
                    ->add('address','collection',array('type'=>new AddressType()))
                    ->add('mail','collection',array('type'=>new MailType()));
            }
            public function configureOptions(OptionsResolver $resolver)
            {
                $resolver->setDefaults(
                    array(
                        'data_class' => 'CustomerBundle\Entity\Customer',
                    )
                );
            }
            public function getName()
            {
                return 'customer';
            }
        }

In the create action of My Controller I added this:

/**
 * @Route("/Customers/Create", name="crm_customers_create")
 * @Template()
 */
public function createAction()
{
    $customer = new Customer();
    $address = new Address();
    $mail = new Mail();
    $customer->addAddress($address);
    $customer->addEmail($mail);
    $form = $this->createForm(new CustomerType(),$customer);
    return array(
        'form'=>$form->createView(),
    );
}

and my Twig Template contains:

{% extends "HomeBundle:Core:base.html.twig" %}

    {% block title %}CustomerBundle:Customers:create{% endblock %}

    {% block body %}
        <div class="col-xs-10 col-xs-offset-1">
            <fieldset>
                <legend>{% trans %}create a new Customer{% endtrans %}</legend>
                {{ form_start(form) }}
                {{ form_widget(form) }}
                {{ form_end(form) }}
            </fieldset>
        </div>
    {% endblock %}

So as soon as I cann the page I only get this output:

Expected argument of type "array or (\Traversable and \ArrayAccess)", "boolean" given 500 Internal Server Error - UnexpectedTypeException Stack Trace

        in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php at line 84  -
                    }
                    if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
                        throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)');
                    }
                    // First remove all rows
        at ResizeFormListener ->preSetData (object(FormEvent), 'form.pre_set_data', object(EventDispatcher))
        at call_user_func (array(object(ResizeFormListener), 'preSetData'), object(FormEvent), 'form.pre_set_data', object(EventDispatcher))
        in app/cache/dev/classes.php at line 1826  +
        at EventDispatcher ->doDispatch (array(array(object(ResizeFormListener), 'preSetData')), 'form.pre_set_data', object(FormEvent))
        in app/cache/dev/classes.php at line 1759  +
        at EventDispatcher ->dispatch ('form.pre_set_data', object(FormEvent))
        in vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php at line 43  +
        at ImmutableEventDispatcher ->dispatch ('form.pre_set_data', object(FormEvent))
        in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 342  +
        at Form ->setData (false)
        in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 57  +
        at PropertyPathMapper ->mapDataToForms (object(Customer), object(RecursiveIteratorIterator))
        in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 396  +
        at Form ->setData (object(Customer))
        in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 488  +
        at Form ->initialize ()
        in vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 226  +
        at FormBuilder ->getForm ()
        in vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 39  +
        at FormFactory ->create (object(CustomerType), object(Customer), array())
        in vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 250  +
        at Controller ->createForm (object(CustomerType), object(Customer))
        in src/CustomerBundle/Controller/CustomersController.php at line 47  +
        at CustomersController ->createAction ()
        at call_user_func_array (array(object(CustomersController), 'createAction'), array())
        in app/bootstrap.php.cache at line 3109  +
        at HttpKernel ->handleRaw (object(Request), '1')
        in app/bootstrap.php.cache at line 3071  +
        at HttpKernel ->handle (object(Request), '1', true)
        in app/bootstrap.php.cache at line 3222  +
        at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
        in app/bootstrap.php.cache at line 2444  +
        at Kernel ->handle (object(Request))
        in web/app_dev.php at line 29  +

I don't know there the issue comes from as I can't see which of the entities causes the fault. Everything looks fine so far. I also tried to access only one of the form fields, but that leed to the same exception. I appreceate any help with solving this issue.

4
  • The Entities are reduced in size so I don't bust the 10.000 Character Limit. I only removed getter and setters as well as add/remove handling from the entities. If needed I can add them as well
    – Chris West
    Commented Aug 16, 2015 at 9:37
  • Just added one complet call for one of the array collections. the others are built similar to the others
    – Chris West
    Commented Aug 16, 2015 at 9:39
  • properties addresses and emails in customer form do not match entity ones. Commented Aug 16, 2015 at 9:51
  • Thank you for your help. Didn't see that. Can you post your Comment as answer, so I can accept it. (Please) ^^
    – Chris West
    Commented Aug 16, 2015 at 9:56

1 Answer 1

2

Form field names must match entity property names. In CustomerType form fields address and email do not match Customer entity properties Addresses and Emails.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.