我对表单收集有疑问。我收到一个错误:
Notice: Array to string conversion in /home/.../vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 103
我尝试了 foreach (就像在教义文档中一样)并保留每个对象,但我收到一个错误:
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
下面是我的代码:
实体:
/**
* @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;
形式:
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,
));
}
控制器:
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();
}
}
}