我正在使用Symfony 2.8
我在用户和扇区实体之间有很多与许多关系,插入效果很好,但是编辑表单(编辑表格函数)的创建不起作用。所有代码都在那里:
<?php
namespace UserBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
class User extends BaseUser implements ParticipantInterface
{
/**
* @ORMId
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMGeneratedValue(strategy="CUSTOM")
* @ORMCustomIdGenerator(class="CoreBundleModelCustomIdGenerator")
*
* @var int
*/
protected $id;
/**
* @ORMManyToMany(targetEntity="UserBundleEntitySectors", fetch="EAGER")
* @ORMJoinTable(
* joinColumns={@ORMJoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORMJoinColumn(onDelete="CASCADE")}
* )
*/
public $sectors;
/**
* @ORMManyToMany(targetEntity="UserBundleEntityUserAccreditation")
* @ORMJoinTable(
* joinColumns={@ORMJoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={@ORMJoinColumn(onDelete="CASCADE")}
* )
*/
public $accreditationdata;
/**
* Constructor.
*/
public function __construct()
{
$this->accreditationdata = new ArrayCollection();
$this->sectors = new ArrayCollection();
parent::__construct();
}
/**
* Add accreditationdatum
*
* @param UserBundleEntityUserAccreditation $accreditationdatum
*
* @return User
*/
public function addAccreditationdatum(UserBundleEntityUserAccreditation $accreditationdatum)
{
$this->accreditationdata[] = $accreditationdatum;
return $this;
}
/**
* Remove accreditationdatum
*
* @param UserBundleEntityUserAccreditation $accreditationdatum
*/
public function removeAccreditationdatum(UserBundleEntityUserAccreditation $accreditationdatum)
{
$this->accreditationdata->removeElement($accreditationdatum);
}
/**
* Add sector
*
* @param UserBundleEntitySectors $sector
*
* @return User
*/
public function addSector(UserBundleEntitySectors $sector)
{
$this->sectors[] = $sector;
return $this;
}
/**
* Remove sector
*
* @param UserBundleEntitySectors $sector
*/
public function removeSector(UserBundleEntitySectors $sector)
{
$this->sectors->removeElement($sector);
}
/**
* Get sectors
*
* @return DoctrineCommonCollectionsCollection
*/
public function getSectors()
{
return $this->sectors;
}
}
profileaboutmeformtype.php
<?php
namespace UserBundleFormType;
use UserBundleEntityUser;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormEvents;
use SymfonyComponentHttpFoundationRequestStack;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyBridgeDoctrineFormTypeEntityType;
use DoctrineORMEntityRepository;
use SymfonyComponentFormFormInterface;
use UserBundleEntitySectors;
use UserBundleEntityUserAccreditation;
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' => 'UserBundleEntitySectors',
'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' => 'UserBundleEntityUserAccreditation',
'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' => 'CocoricoUserBundleEntityUserModel',
'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, DoctrineCommonCollectionsArrayCollection $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec->id;
}
$formOption = array(
'class' => 'UserBundleEntityUserAccreditation',
'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';
}
}
我会收到错误,说:
类型错误:参数2传递给UserBundle form type prifileaBoutMeformType :: UserBundle form form form type type {clocture}()必须是教义 common common collections collections collections arraycollection arraycollection,of doctrine orm orm orm orm orm orm orm orm orm orm orm ,in/src/userbundle/form/type/profileaboutmeformtype.php in 383
我应该更改getectors方法以持久汇总吗?或者我需要完全改变我的方法?
所以,是的,基本上回答了我自己的问题。我为这种方法提供了自己的解决方案。我必须更改"编辑表单"的EventListener。
正如我通过存储库查询数据库时所了解的,您会得到 持久票据以及与实体合作时,您会得到 ArrayCollection
我已将 Doctrine common collections arrayCollection $ sector作为$ formmodify函数中的数组$ sector。另外,我也在pre_set_data和postrongubmit事件中更改。
以下是更改事件侦听器的完整代码。
$factory = $builder->getFormFactory();
$formModify = function (FormInterface $form, array $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec;
}
$formOption = array(
'class' => 'CocoricoUserBundleEntityUserAccreditation',
'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);
}
);
这对我有用。希望它可以帮助其他人采用这种类型的方法。