[英]Symfony3 Error on flush with entity embeddedin form
我创建了一个“ClientType”与“信息类型”和“ContactType”。 客户可以有多种信息和联系方式,因此我使用嵌入客户,信息和联系方式的表格 。
当我打印表格时可以,但是当我刷新时:
可捕获的致命错误:传递给Doctrine \\ Common \\ Collections \\ ArrayCollection :: __ construct()的参数1必须为数组类型,给定对象,在C:\\ wamp \\ www \\ LinkWebCRM \\ vendor \\ doctrine \\ orm \\ lib \\ Doctrine中调用\\ ORM \\ UnitOfWork.php在第605行并已定义
我尝试使用“ InformationType”和“ ContactType”的“ CollectionType”,但随后收到很多表单警告“此值无效。”,“此表单不应包含其他字段。”,并且是该表单的8倍信息和联系方式...
客户类型:
class ClientType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom', TextType::class);
$builder->add('prenom', TextType::class);
$builder->add('informations', InformationType::class);
$builder->add('contacts', ContactType::class);
$builder->add('Enregistrer', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CommonBundle\Entity\Client',
));
}
}
信息:
class InformationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nomEntreprise', TextType::class);
$builder->add('sIRET', TextType::class);
$builder->add('typeSite', TextType::class);
$builder->add('adresseEntreprise', TextType::class);
$builder->add('dateSignature', DateTimeType::class, array(
'years' => range(date("Y"), date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 1825 day")))
));
$builder->add('nomDomaine', TextType::class);
$builder->add('dateMiseEnLigne', DateTimeType::class, array(
'years' => range(date("Y"), date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 1825 day")))
));
$builder->add('commentaire', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('data_class' => null));
}
}
联系人类型:
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('telephone', TextType::class);
$builder->add('fax', TextType::class, array(
'required' => false));
$builder->add('email', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CommonBundle\Entity\Contact',
));
}
}
接着,
客户:
namespace CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use CommonBundle\Entity\Information;
/**
* Client
*
* @ORM\Table(name="client")
* @ORM\Entity(repositoryClass="CommonBundle\Repository\ClientRepository")
*/
class Client
{
/**
* @ORM\OneToMany(targetEntity="CommonBundle\Entity\Information", mappedBy="client")
*/
public $informations;
/**
* @ORM\OneToMany(targetEntity="CommonBundle\Entity\Contact", mappedBy="client")
*/
public $contacts;
/**
* Constructor
*/
public function __construct()
{
$this->informations = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add information
*
* @param \CommonBundle\Entity\Information $information
*
* @return Client
*/
public function addInformation(\CommonBundle\Entity\Information $information)
{
$this->informations[] = $information;
return $this;
}
/**
* Remove information
*
* @param \CommonBundle\Entity\Information $information
*/
public function removeInformation(\CommonBundle\Entity\Information $information)
{
$this->informations->removeElement($information);
}
/**
* Get informations
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getInformations()
{
return $this->informations;
}
/**
* Add contact
*
* @param \CommonBundle\Entity\Contact $contact
*
* @return Client
*/
public function addContact(\CommonBundle\Entity\Contact $contact)
{
$this->contacts[] = $contact;
return $this;
}
/**
* Remove contact
*
* @param \CommonBundle\Entity\Contact $contact
*/
public function removeContact(\CommonBundle\Entity\Contact $contact)
{
$this->contacts->removeElement($contact);
}
/**
* Get contacts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContacts()
{
return $this->contacts;
}
}
信息:
namespace CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use CommonBundle\Entity\Client;
/**
* Information
*
* @ORM\Table(name="information")
* @ORM\Entity(repositoryClass="CommonBundle\Repository\InformationRepository")
*/
class Information
{
/**
* @ORM\ManyToOne(targetEntity="CommonBundle\Entity\Client", inversedBy="informations")
* @ORM\JoinColumn(nullable=false)
*/
public $client;
/**
* Set client
*
* @param \CommonBundle\Entity\Client $client
*
* @return Information
*/
public function setClient(\CommonBundle\Entity\Client $client)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* @return \CommonBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
}
并联系:
namespace CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Contact
*
* @ORM\Table(name="contact")
* @ORM\Entity(repositoryClass="CommonBundle\Repository\ContactRepository")
*/
class Contact
{
/**
* @ORM\ManyToOne(targetEntity="CommonBundle\Entity\Client", inversedBy="contacts")
* @ORM\JoinColumn(nullable=true)
*/
public $client;
/**
* Set client
*
* @param \CommonBundle\Entity\Client $client
*
* @return Contact
*/
public function setClient(\CommonBundle\Entity\Client$client = null)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* @return \CommonBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
}
因此,由于集合的原因,我不得不创建一个Information实例。 然后我像这样水合:
$information->setNameVariable($client->getInformations()['nameVariable'])
注意,$ client-> getInformations()中的“信息”目前不是对象,而是表单的数组,这就是问题所在。
对于表格中的所有变量,这与预期相反。 最后,我添加了var不可为空的变量,保留对象, 对“ Contact”重复逻辑 ,然后刷新。
我还必须添加Client的__constructor :
$this->contacts = new \Doctrine\Common\Collections\ArrayCollection();
对于ClientType:
$builder->add('informations', InformationType::class, array(
'data_class' => null));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.