预期的 Symfony 必须是 Doctrine\Common\Collections\ArrayCollection 的实例,给定 Doctrine\ORM\PersistentCollection 的实例
Symfony expected must be an instance of Doctrine\Common\Collections\ArrayCollection, instance of Doctrine\ORM\PersistentCollection given
I am using Symfony 2.8
我在 User 和 Sectors 实体之间存在多对多关系,插入效果很好,但编辑表单(editAction 函数)的创建不起作用。所有代码都在那里:
<?php
namespace UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
class User extends BaseUser implements ParticipantInterface
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="CoreBundle\Model\CustomIdGenerator")
*
* @var int
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="UserBundle\Entity\Sectors", fetch="EAGER")
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")}
* )
*/
public $sectors;
/**
* @ORM\ManyToMany(targetEntity="UserBundle\Entity\UserAccreditation")
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")}
* )
*/
public $accreditationdata;
/**
* Constructor.
*/
public function __construct()
{
$this->accreditationdata = new ArrayCollection();
$this->sectors = new ArrayCollection();
parent::__construct();
}
/**
* Add accreditationdatum
*
* @param UserBundle\Entity\UserAccreditation $accreditationdatum
*
* @return User
*/
public function addAccreditationdatum(UserBundle\Entity\UserAccreditation $accreditationdatum)
{
$this->accreditationdata[] = $accreditationdatum;
return $this;
}
/**
* Remove accreditationdatum
*
* @param UserBundle\Entity\UserAccreditation $accreditationdatum
*/
public function removeAccreditationdatum(UserBundle\Entity\UserAccreditation $accreditationdatum)
{
$this->accreditationdata->removeElement($accreditationdatum);
}
/**
* Add sector
*
* @param UserBundle\Entity\Sectors $sector
*
* @return User
*/
public function addSector(UserBundle\Entity\Sectors $sector)
{
$this->sectors[] = $sector;
return $this;
}
/**
* Remove sector
*
* @param UserBundle\Entity\Sectors $sector
*/
public function removeSector(UserBundle\Entity\Sectors $sector)
{
$this->sectors->removeElement($sector);
}
/**
* Get sectors
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSectors()
{
return $this->sectors;
}
}
ProfileAboutMeFormType.php
<?php
namespace UserBundle\Form\Type;
use UserBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormInterface;
use UserBundle\Entity\Sectors;
use UserBundle\Entity\UserAccreditation;
class ProfileAboutMeFormType extends AbstractType
{
/**
* @param string $class The User class name
* @param RequestStack $requestStack
* @param array $locales
*/
public function __construct($class, RequestStack $requestStack, $locales)
{
$this->class = $class;
$this->request = $requestStack->getCurrentRequest();
$this->locale = $this->request->getLocale();
$this->locales = $locales;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
/** @var User $user */
$user = $builder->getData();
$builder
->add('sectors', EntityType::class, array(
'class' => 'UserBundle\Entity\Sectors',
'placeholder' => 'Select Sector ...',
'label' => 'Sector',
'required' => false,
'multiple' => false,
'expanded' => false,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
))
->add('accreditationdata', EntityType::class, array(
'class' => 'UserBundle\Entity\UserAccreditation',
'label' => 'Accreditation',
'required' => false,
'multiple' => true,
'expanded' => true,
))
;
}
$formModifier = function (FormInterface $form, UserMake $make = null) {
$maker = null === $make ? array() : $make->getId();
$form->add('model', EntityType::class, array(
'class' => 'Cocorico\UserBundle\Entity\UserModel',
'query_builder' => function(EntityRepository $ert) use ($maker) {
$query = $ert->createQueryBuilder('i')
->select(array('i'))
->where('i.make = :make_id')
->setParameter('make_id', $maker)
->orderBy('i.name', 'ASC');
return $query;
},
//'choices' => array($maker),
'required' => false,
'placeholder' => 'Select Make First ...',
'multiple' => false,
'expanded' => false,
)
);
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null)
$formModifier($event->getForm(), $data->getMakes());
}
);
$builder->get('makes')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModifier) {
$make = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $make);
}
);
/******************************** This event listener is for sector and accreditation dependent fields ***************************************/
$factory = $builder->getFormFactory();
$formModify = function (FormInterface $form, \Doctrine\Common\Collections\ArrayCollection $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec->id;
}
$formOption = array(
'class' => 'UserBundle\Entity\UserAccreditation',
'multiple' => true,
'auto_initialize' => false,
'required' => false,
'expanded' => true,
'choice_attr' => function ($output) {
return ['class' => 'attr_checkbox'];
},
'query_builder' => function(EntityRepository $ertt) use ($output) {
// build a custom query, or call a method on your repository (even better!)
$qb = $ertt->createQueryBuilder('g');
$qb->select(array('g'));
$qb->where('g.sector IN (:sector_id)');
$qb->setParameters( array('sector_id' => $output) );
$qb->orderBy('g.name', 'ASC');
return $qb;
},
);
$form->add($factory->createNamed('accreditationdata', EntityType::class, null, $formOption));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModify,$factory) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null)
//print_r(get_object_vars($data->getSectors()));
$formModify($event->getForm(), $data->getSectors(),$factory);
}
);
$factory = $builder->getFormFactory();
$builder->get('sectors')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModify,$factory) {
$sector = $event->getForm()->getData();
$formModify($event->getForm()->getParent(), $sector,$factory);
}
);
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => $this->class,
'csrf_token_id' => 'profile',
'translation_domain' => 'user',
)
);
}
/**
* BC
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'user_profile_about_me';
}
}
我收到错误消息:
Type error: Argument 2 passed to UserBundle\Form\Type\ProfileAboutMeFormType::UserBundle\Form\Type{closure}() must be an instance of Doctrine\Common\Collections\ArrayCollection, instance of Doctrine\ORM\PersistentCollection given, called in /src/UserBundle/Form/Type/ProfileAboutMeFormType.php on line 383
我应该将 getSectors 方法更改为 PersistentCollection 吗??或者我需要完全改变我的方法?
是的,基本上回答了我自己的问题。对于这种方法,我有自己的解决方案。我必须更改 "Edit Form".
的事件监听器
As I understood when you query database by repository you get
PersistentCollection and when your working with your entities you get
ArrayCollection
我在 $formModify 函数中将 \Doctrine\Common\Collections\ArrayCollection $sector 更改为数组 $sector。我也在 PRE_SET_DATA 和 POST_SUBMIT 事件中进行了更改。
下面是更改事件监听器的完整代码。
$factory = $builder->getFormFactory();
$formModify = function (FormInterface $form, array $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec;
}
$formOption = array(
'class' => 'Cocorico\UserBundle\Entity\UserAccreditation',
'multiple' => true,
'auto_initialize' => false,
'required' => false,
'expanded' => true,
'label' => 'Accreditations',
'choice_attr' => function ($output) {
return ['class' => 'attr_checkbox'];
},
'query_builder' => function(EntityRepository $ertt) use ($output) {
// build a custom query, or call a method on your repository (even better!)
$qb = $ertt->createQueryBuilder('g');
$qb->select(array('g'));
$qb->where('g.sector IN (:sector_id)');
$qb->setParameters( array('sector_id' => $output) );
$qb->orderBy('g.name', 'ASC');
return $qb;
},
);
$form->add($factory->createNamed('accreditationdata', EntityType::class, null, $formOption));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModify,$factory) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null)
$newarray = [];
foreach ($data->getSectors() as $post) {
// $post is not an array
$newarray[] = $post->id;
}
$formModify($event->getForm(), $newarray,$factory);
}
);
$factory = $builder->getFormFactory();
$builder->get('sectors')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModify,$factory) {
$sector = $event->getForm()->getData();
$created_array = array($sector->getId());
$array = [];
foreach($created_array as $secval) {
$array[] = $secval;
}
$formModify($event->getForm()->getParent(), $array,$factory);
}
);
这对我有用。希望它能帮助其他人使用这种方法。
I am using Symfony 2.8
我在 User 和 Sectors 实体之间存在多对多关系,插入效果很好,但编辑表单(editAction 函数)的创建不起作用。所有代码都在那里:
<?php
namespace UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
class User extends BaseUser implements ParticipantInterface
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="CoreBundle\Model\CustomIdGenerator")
*
* @var int
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="UserBundle\Entity\Sectors", fetch="EAGER")
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")}
* )
*/
public $sectors;
/**
* @ORM\ManyToMany(targetEntity="UserBundle\Entity\UserAccreditation")
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")}
* )
*/
public $accreditationdata;
/**
* Constructor.
*/
public function __construct()
{
$this->accreditationdata = new ArrayCollection();
$this->sectors = new ArrayCollection();
parent::__construct();
}
/**
* Add accreditationdatum
*
* @param UserBundle\Entity\UserAccreditation $accreditationdatum
*
* @return User
*/
public function addAccreditationdatum(UserBundle\Entity\UserAccreditation $accreditationdatum)
{
$this->accreditationdata[] = $accreditationdatum;
return $this;
}
/**
* Remove accreditationdatum
*
* @param UserBundle\Entity\UserAccreditation $accreditationdatum
*/
public function removeAccreditationdatum(UserBundle\Entity\UserAccreditation $accreditationdatum)
{
$this->accreditationdata->removeElement($accreditationdatum);
}
/**
* Add sector
*
* @param UserBundle\Entity\Sectors $sector
*
* @return User
*/
public function addSector(UserBundle\Entity\Sectors $sector)
{
$this->sectors[] = $sector;
return $this;
}
/**
* Remove sector
*
* @param UserBundle\Entity\Sectors $sector
*/
public function removeSector(UserBundle\Entity\Sectors $sector)
{
$this->sectors->removeElement($sector);
}
/**
* Get sectors
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSectors()
{
return $this->sectors;
}
}
ProfileAboutMeFormType.php
<?php
namespace UserBundle\Form\Type;
use UserBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormInterface;
use UserBundle\Entity\Sectors;
use UserBundle\Entity\UserAccreditation;
class ProfileAboutMeFormType extends AbstractType
{
/**
* @param string $class The User class name
* @param RequestStack $requestStack
* @param array $locales
*/
public function __construct($class, RequestStack $requestStack, $locales)
{
$this->class = $class;
$this->request = $requestStack->getCurrentRequest();
$this->locale = $this->request->getLocale();
$this->locales = $locales;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
/** @var User $user */
$user = $builder->getData();
$builder
->add('sectors', EntityType::class, array(
'class' => 'UserBundle\Entity\Sectors',
'placeholder' => 'Select Sector ...',
'label' => 'Sector',
'required' => false,
'multiple' => false,
'expanded' => false,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
))
->add('accreditationdata', EntityType::class, array(
'class' => 'UserBundle\Entity\UserAccreditation',
'label' => 'Accreditation',
'required' => false,
'multiple' => true,
'expanded' => true,
))
;
}
$formModifier = function (FormInterface $form, UserMake $make = null) {
$maker = null === $make ? array() : $make->getId();
$form->add('model', EntityType::class, array(
'class' => 'Cocorico\UserBundle\Entity\UserModel',
'query_builder' => function(EntityRepository $ert) use ($maker) {
$query = $ert->createQueryBuilder('i')
->select(array('i'))
->where('i.make = :make_id')
->setParameter('make_id', $maker)
->orderBy('i.name', 'ASC');
return $query;
},
//'choices' => array($maker),
'required' => false,
'placeholder' => 'Select Make First ...',
'multiple' => false,
'expanded' => false,
)
);
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null)
$formModifier($event->getForm(), $data->getMakes());
}
);
$builder->get('makes')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModifier) {
$make = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $make);
}
);
/******************************** This event listener is for sector and accreditation dependent fields ***************************************/
$factory = $builder->getFormFactory();
$formModify = function (FormInterface $form, \Doctrine\Common\Collections\ArrayCollection $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec->id;
}
$formOption = array(
'class' => 'UserBundle\Entity\UserAccreditation',
'multiple' => true,
'auto_initialize' => false,
'required' => false,
'expanded' => true,
'choice_attr' => function ($output) {
return ['class' => 'attr_checkbox'];
},
'query_builder' => function(EntityRepository $ertt) use ($output) {
// build a custom query, or call a method on your repository (even better!)
$qb = $ertt->createQueryBuilder('g');
$qb->select(array('g'));
$qb->where('g.sector IN (:sector_id)');
$qb->setParameters( array('sector_id' => $output) );
$qb->orderBy('g.name', 'ASC');
return $qb;
},
);
$form->add($factory->createNamed('accreditationdata', EntityType::class, null, $formOption));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModify,$factory) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null)
//print_r(get_object_vars($data->getSectors()));
$formModify($event->getForm(), $data->getSectors(),$factory);
}
);
$factory = $builder->getFormFactory();
$builder->get('sectors')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModify,$factory) {
$sector = $event->getForm()->getData();
$formModify($event->getForm()->getParent(), $sector,$factory);
}
);
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => $this->class,
'csrf_token_id' => 'profile',
'translation_domain' => 'user',
)
);
}
/**
* BC
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'user_profile_about_me';
}
}
我收到错误消息:
Type error: Argument 2 passed to UserBundle\Form\Type\ProfileAboutMeFormType::UserBundle\Form\Type{closure}() must be an instance of Doctrine\Common\Collections\ArrayCollection, instance of Doctrine\ORM\PersistentCollection given, called in /src/UserBundle/Form/Type/ProfileAboutMeFormType.php on line 383
我应该将 getSectors 方法更改为 PersistentCollection 吗??或者我需要完全改变我的方法?
是的,基本上回答了我自己的问题。对于这种方法,我有自己的解决方案。我必须更改 "Edit Form".
的事件监听器As I understood when you query database by repository you get PersistentCollection and when your working with your entities you get ArrayCollection
我在 $formModify 函数中将 \Doctrine\Common\Collections\ArrayCollection $sector 更改为数组 $sector。我也在 PRE_SET_DATA 和 POST_SUBMIT 事件中进行了更改。
下面是更改事件监听器的完整代码。
$factory = $builder->getFormFactory();
$formModify = function (FormInterface $form, array $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec;
}
$formOption = array(
'class' => 'Cocorico\UserBundle\Entity\UserAccreditation',
'multiple' => true,
'auto_initialize' => false,
'required' => false,
'expanded' => true,
'label' => 'Accreditations',
'choice_attr' => function ($output) {
return ['class' => 'attr_checkbox'];
},
'query_builder' => function(EntityRepository $ertt) use ($output) {
// build a custom query, or call a method on your repository (even better!)
$qb = $ertt->createQueryBuilder('g');
$qb->select(array('g'));
$qb->where('g.sector IN (:sector_id)');
$qb->setParameters( array('sector_id' => $output) );
$qb->orderBy('g.name', 'ASC');
return $qb;
},
);
$form->add($factory->createNamed('accreditationdata', EntityType::class, null, $formOption));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModify,$factory) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null)
$newarray = [];
foreach ($data->getSectors() as $post) {
// $post is not an array
$newarray[] = $post->id;
}
$formModify($event->getForm(), $newarray,$factory);
}
);
$factory = $builder->getFormFactory();
$builder->get('sectors')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModify,$factory) {
$sector = $event->getForm()->getData();
$created_array = array($sector->getId());
$array = [];
foreach($created_array as $secval) {
$array[] = $secval;
}
$formModify($event->getForm()->getParent(), $array,$factory);
}
);
这对我有用。希望它能帮助其他人使用这种方法。