Symfony 2.8 表单实体类型自定义 属性
Symfony 2.8 form entity type custom property
我正在 Symfony 2.8 应用程序中处理表单。
我有一个实体对象,该实体可以有一个或多个子对象。
这些子对象由 属性 id 标识,但也由 属性 key.
标识
默认情况下,id 属性 中的值用于 HTML (subObject.__toString())。我想在 .
中使用 属性 key
我似乎找不到该怎么做...
PS:我不能使用 SubObject 的 __toString() 方法,因为它已经被用于其他目的...
将不胜感激。
<?php
namespace My\Bundle\ObjectBundle\Form\Type\Object;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class ObjectType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code', TextType::class, [
'required' => true,
])
->add('subObjects', EntityType::class, [
'class' => 'My\Bundle\ObjectBundle\Entity\SubObject',
'multiple' => true,
])
}
}
我快速编写了一段关于如何在侦听器中执行此操作的伪代码,希望我能理解您的需求。无论如何这是一个通用的方法。
class ResolveSubObjectSubscriber implements EventSubscriberInterface {
/** @var EntityManager */
private $entityManager;
public function __construct(FormFactoryInterface $factory, EntityManager $entityManager) {
$this->factory = $factory;
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents() {
return array(FormEvents::POST_SET_DATA => 'resolveSubObject');
}
/**
* Resolve sub objects based on key
*
* @param FormEvent $event
*/
public function resolveSubObject(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
// don't care if it's not a sub object
if (!$data instanceof SubObject) {
return;
}
/** @var SubObject $subObject */
$subObject = $data;
// here you do whatever you need to do to populate the object into the sub object based on key
$subObjectByKey = $this->entityManager->getRepository('SomeRepository')->findMySubObject($subObject->getKey());
$subObject->setObject($subObjectByKey);
}
}
我正在 Symfony 2.8 应用程序中处理表单。
我有一个实体对象,该实体可以有一个或多个子对象。 这些子对象由 属性 id 标识,但也由 属性 key.
标识默认情况下,id 属性 中的值用于 HTML (subObject.__toString())。我想在 .
中使用 属性 key我似乎找不到该怎么做...
PS:我不能使用 SubObject 的 __toString() 方法,因为它已经被用于其他目的...
将不胜感激。
<?php
namespace My\Bundle\ObjectBundle\Form\Type\Object;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class ObjectType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code', TextType::class, [
'required' => true,
])
->add('subObjects', EntityType::class, [
'class' => 'My\Bundle\ObjectBundle\Entity\SubObject',
'multiple' => true,
])
}
}
我快速编写了一段关于如何在侦听器中执行此操作的伪代码,希望我能理解您的需求。无论如何这是一个通用的方法。
class ResolveSubObjectSubscriber implements EventSubscriberInterface {
/** @var EntityManager */
private $entityManager;
public function __construct(FormFactoryInterface $factory, EntityManager $entityManager) {
$this->factory = $factory;
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents() {
return array(FormEvents::POST_SET_DATA => 'resolveSubObject');
}
/**
* Resolve sub objects based on key
*
* @param FormEvent $event
*/
public function resolveSubObject(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
// don't care if it's not a sub object
if (!$data instanceof SubObject) {
return;
}
/** @var SubObject $subObject */
$subObject = $data;
// here you do whatever you need to do to populate the object into the sub object based on key
$subObjectByKey = $this->entityManager->getRepository('SomeRepository')->findMySubObject($subObject->getKey());
$subObject->setObject($subObjectByKey);
}
}