Goku
Goku

Reputation: 2139

Many to one relation : ArrayCollection issue

Domain form

I have a many to one relation between Domain and Site. But when I try to add a new domain I have this error :

Found entity of type Doctrine\Common\Collections\ArrayCollection on association Eliophot\BackBundle\Entity\Domain#site, but expecting Eliophot\BackBundle\Entity\Site

This is my DomainController :

public function createDomainAction(Request $request)
    {
        $domain = new Domain;

        if (!$domain) {
            throw $this->createNotFoundException('Unable to find Domain entity.');
        }

        $newForm = $this->createForm(new DomainType(), $domain);

        $newForm->handleRequest($request);

        if ($newForm->isValid()) {
            $entityManager = $this->get('doctrine')->getManager();
            $entityManager->persist($domain);
            $entityManager->flush();
            $this->get('session')->getFlashBag()->add('success', 'Le domaine a été crée');

            return $this->redirect($this->generateUrl('domain_list'));
        }

        return $this->render('EliophotBackBundle:Domain:new_domain.html.twig', array(
            'domain'   => $domain,
            'form'   => $newForm->createView(),
        ));
    }

This is my DomainType:

class DomainType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('domainName','text', array(
                'label' => 'Nom du domaine'
            ))
            ->add('site','entity', array(
                'class' => 'EliophotBackBundle:Site',
                'property' => 'name',
                'label' => 'Sélectionnez un ou plusieurs site(s)',
                'multiple' => true
            ));
    }

    public function getName()
    {
        return 'domain';
    }
...
}

and my SiteType:

class SiteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text', array(
                'label' => 'Nom du site',
                'required' => true
            ))
            ->add('nameBundle', 'text', array(
                'label' => 'Nom du bundle du site',
                'required' => true
            ))
            ->add('numClient', 'integer', array(
                'label' => 'Numéro client du site',
                'required' => true
            ));
    }

    public function getName()
    {
        return 'site';
    }
...
}

Site entity :

/**
 * Site
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Eliophot\BackBundle\Entity\SiteRepository")
 */
class Site
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="name_bundle", type="string", length=255)
     */
    private $nameBundle;

    /**
     * @var integer
     *
     * @ORM\Column(name="num_client", type="integer")
     */
    private $numClient;

    /**
     * @ORM\OneToMany(targetEntity="Domain", mappedBy="site")
     */
    protected $domains;

    /**
     * Initialisation
     */
    public function __construct()
    {
        $this->domains = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Site
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set nameBundle
     *
     * @param $nameBundle
     * @return $this
     */
    public function setNameBundle($nameBundle)
    {
        $this->nameBundle = $nameBundle;

        return $this;
    }

    /**
     * Get nameBundle
     *
     * @return string
     */
    public function getNameBundle()
    {
        return $this->nameBundle;
    }

    /**
     * Set numClient
     *
     * @param integer $numClient
     * @return Site
     */
    public function setNumClient($numClient)
    {
        $this->numClient = $numClient;

        return $this;
    }

    /**
     * Get numClient
     *
     * @return integer 
     */
    public function getNumClient()
    {
        return $this->numClient;
    }

    /**
     * Get domains
     *
     * @return ArrayCollection
     */
    public function getDomains()
    {
        return $this->domains;
    }

    /**
     * Set domains
     *
     * @param ArrayCollection $domains
     */
    public function setDomains(ArrayCollection $domains)
    {
        $this->domains = $domains;
    }
}

Domain entity :

/**
 * Domain
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Domain
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="domain_name", type="string", length=255)
     */
    private $domainName;

    /**
     * @ORM\ManyToOne(targetEntity="Site", inversedBy="domains")
     * @ORM\JoinColumn(name="site_id", referencedColumnName="id")
     */
    protected $site;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set domainName
     *
     * @param string $domainName
     * @return Domain
     */
    public function setDomainName($domainName)
    {
        $this->domainName = $domainName;

        return $this;
    }

    /**
     * Get domainName
     *
     * @return string 
     */
    public function getDomainName()
    {
        return $this->domainName;
    }

    public function getSite()
    {
        return $this->site;
    }

    public function setSite($site)
    {
        $this->site = $site;

        return $this;
    }
}

Like the error says, I think Symfony expects a Site Object and not an arrayCollection... when I var_dump the request after the adding, I get this for the site field (in this example I have selected "site de test 1" and "site de test 2") :

array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }

=> this should be added to the domain table 2 new lines for the domain in question

So how can I do this ?

Upvotes: 0

Views: 128

Answers (1)

sebbo
sebbo

Reputation: 2939

It seems that you are trying to associate the objects the other way around. Your frontend shows a form where the user is able to enter one domain and select the sites that should be associated with this domain.

From my point of view you should create a form where the user types in the site name and selects the domains to be associated. Just the other way around.

Upvotes: 1

Related Questions