当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Symfony Form collections flush() error

问题描述:

I have problem with form collection. I get an error:

Notice: Array to string conversion in /home/.../vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 103

i tried foreach (like is in doctrine documentation) and perist each object, but I get an error:

The class 'Doctrine\Common\Collections\ArrayCollection' was not found in the chain configured namespaces FOS\UserBundle\Entity, Ix\UserBundle\Entity, Ix\x\Entity, FOS\UserBundle\Model

Below is my code:

Entity:

/**

* @ORM\Entity

* @ORM\Table(name="availability")

*/

class Availability

{

public function __construct()

{

$this->availabilityFlexible = new \Doctrine\Common\Collections\ArrayCollection();

$this->availabilitySession = new \Doctrine\Common\Collections\ArrayCollection();

}

/**

* @ORM\Id

* @ORM\Column(type="integer")

* @ORM\GeneratedValue(strategy="AUTO")

*/

protected $id;

/**

* @ORM\Column(name="description", type="string", length=255, nullable=true)

*/

protected $description;

/**

* @ORM\OneToMany(targetEntity="AvailabilityFlexible", mappedBy="availability", cascade={"persist", "remove"})

*/

protected $availabilityFlexible;

/**

* @ORM\OneToOne(targetEntity="AvailabilityFull", mappedBy="availability", cascade={"persist", "remove"})

*/

protected $availabilityFull;

/**

* @ORM\OneToMany(targetEntity="AvailabilitySession", mappedBy="availability", cascade={"persist", "remove"})

*/

protected $availabilitySession;

// AvailabilityFlexible.php

/**

* @ORM\ManyToOne(targetEntity="Availability", inversedBy="availabilityFlexible", cascade={"persist"})

* @ORM\JoinColumn(name="availability_id", referencedColumnName="id")

*/

protected $availability;

// AvailabilityFull.php

/**

* @ORM\OneToOne(targetEntity="Availability", inversedBy="availabilityFull", cascade={"persist"})

* @ORM\JoinColumn(name="availability_id", referencedColumnName="id")

*/

protected $availability;

// AvailabilitySession.php

/**

* @ORM\ManyToOne(targetEntity="Availability", inversedBy="availabilitySession", cascade={"persist"})

* @ORM\JoinColumn(name="availability_id", referencedColumnName="id")

*/

protected $availability;

Form:

 public function buildForm(FormBuilderInterface $builder, array $options)

{

$builder->add('description')

->add('AvailabilitySession', 'collection', array(

'type' => new AvailabilitySessionFormType(),

'allow_add' => true,

'prototype' => true,

'by_reference' => false,

'allow_delete' => true,

))

->add('AvailabilityFull', new AvailabilityFullFormType())

->add('AvailabilityFlexible', 'collection', array(

'type' => new AvailabilityFlexibleFormType(),

'allow_add' => true,

'by_reference' => false,

'allow_delete' => true,

));

}

Controller:

 public function addAvailabilityAction(Request $request)

{

$availability = new Availability;

$availability->getAvailabilityFlexible()->add(new AvailabilityFlexible);

$availability->getAvailabilityFlexible()->add(new AvailabilityFlexible);

$form = $this->createForm(new AvailabilityFormType(), $availability);

if ($request->isMethod('POST')) {

$form->bind($request);

if ($form->isValid()) {

$availability = $form->getData();

$em = $this->getDoctrine()->getManager();

$em->perist($availability);

$em->flush();

}

}

}

网友答案:

Properties $availability are in different class.

  1. AvailabilityFlexible.php
  2. AvailabilityFull.php
  3. AvailabilitySession.php

This is relationship to Availability Entity.

分享给朋友:
您可能感兴趣的文章:
随机阅读: