양식에 entity
유형을 사용한다고 가정합니다. 처음 엔 모든 엔티티가 객체로 가져온 다음 약간의 id => label
스타일로 축소 되었기 때문에 매우 무겁습니다.
그래서 (그래서 아무것도 프리스트 장소에 개체로 반입하지 않습니다)는 id => label
-array와 함께 작동 자신의 entityChoice
유형을 작성하고이 유형에 DataTransformer를 추가 할 수 있습니다
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use MyNamespace\EntityToIdTransformer;
class EntityChoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new EntityToIdTransformer($options['repository']));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'empty_value' => false,
'empty_data' => null,
));
$resolver->setRequired(array(
'repository'
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'entityChoice';
}
}
등 DataTransformer :
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class EntityToIdTransformer implements DataTransformerInterface
{
private $entityRepository;
public function __construct(EntityRepository $entityRepository)
{
$this->entityRepository = $entityRepository;
}
/**
* @param object|array $entity
* @return int|int[]
*
* @throws TransformationFailedException
*/
public function transform($entity)
{
if ($entity === null) {
return null;
}
elseif (is_array($entity) || $entity instanceof \Doctrine\ORM\PersistentCollection) {
$ids = array();
foreach ($entity as $subEntity) {
$ids[] = $subEntity->getId();
}
return $ids;
}
elseif (is_object($entity)) {
return $entity->getId();
}
throw new TransformationFailedException((is_object($entity)? get_class($entity) : '').'('.gettype($entity).') is not a valid class for EntityToIdTransformer');
}
/**
* @param int|array $id
* @return object|object[]
*
* @throws TransformationFailedException
*/
public function reverseTransform($id)
{
if ($id === null) {
return null;
}
elseif (is_numeric($id)) {
$entity = $this->entityRepository->findOneBy(array('id' => $id));
if ($entity === null) {
throw new TransformationFailedException('A '.$this->entityRepository->getClassName().' with id #'.$id.' does not exist!');
}
return $entity;
}
elseif (is_array($id)) {
if (empty($id)) {
return array();
}
$entities = $this->entityRepository->findBy(array('id' => $id)); // its array('id' => array(...)), resulting in many entities!!
if (count($id) != count($entities)) {
throw new TransformationFailedException('Some '.$this->entityRepository->getClassName().' with ids #'.implode(', ', $id).' do not exist!');
}
return $entities;
}
throw new TransformationFailedException(gettype($id).' is not a valid type for EntityToIdTransformer');
}
}
을 그리고 마지막으로 service.yml의 새로운 유형으로 FormType을 등록
services:
myNamespace.form.type.entityChoice:
class: MyNamespace\EntityChoiceType
tags:
- { name: form.type, alias: entityChoice }
그런 다음 '형식을 사용하지 않는 id => label
출처
2014-10-07 11:15:46
SBH
으로 배열로 원하는 저장소 및
'choices'
의 인스턴스로$repository
와로 형태로 사용할 수 있습니다 entity' 그러나 다만'선택 'id => label'을 사용하고 [DataTransformers] (http://symfony.com/doc/current/cookbook/form/data_transformers.html)를 작성하십시오. 그런 다음 무언가를 선택했을 때 최종 값으로 만 작업합니다. – SBH