duanpi7107
2017-02-07 13:24
浏览 31
已采纳

Symfony表单类型实体数据transformerm

I have form with field type entity on this field I have query_builder which return query and 'property' => 'name',. And my question what need to do in data transformer for change select name, need complicated with several filed, example - name_address_office. Using Symfony 2.8 I need dataTransformer approach

my form

class OutBoundInvoiceRowType extends AbstractType
{
/**
 * @var array
 */
private $vatClasses;

/**
 * @var Container
 */
private $container;

/**
 * @var EntityManager
 */
private $em;

/**
 * OutBoundInvoiceRowType constructor.
 * @param Container $container
 * @param $vatClasses
 */
public function __construct(
    Container $container,    
    $vatClasses
) {
    $this->container = $container;
    $this->vatClasses = $vatClasses;
    $this->em = $this->container->get('doctrine.orm.entity_manager');
}

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('location', 'entity', array(
            'class' => Location::class,
            'property' => 'name',
            'empty_value' => 'Choice Location',
            'query_builder' => self::getLocations(),
            'required' => false
        ))          
        ->add('vat', ChoiceType::class, [
            'choices' => ?,
            'required' => true,
        ])
    $builder->get('vat')
        ->addModelTransformer(new VatTransformer($this->container));
}

and my VatTransformer:

class VatTransformer implements DataTransformerInterface
{
    /**
     * @var Container
     */
    private $container;

    /**
     * @var EntityManager
     */
    private $em;

    /**
     * LocationTransformer constructor.
     * @param Container $container
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
        $this->em = $this->container->get('doctrine.orm.entity_manager');
    }

    /**
     *
     * @param  Location|null $issue
     * @return string
     */
    public function transform($issue)
    {
        if (null === $issue) {
            return '';
        }
    }
}

in function transform $issue have null and when return '' nothing change in form, still have 'property' => 'name', on choice, What need to do in data transform name ?

this now I have

enter image description here

and this what I need

enter image description here

need name of several parts

UPDATE

Ok. I have choice field vat and I need build data in choice for vat field like - from some entity field, example entity Location (id, name)

How this realized with dataTransformer ?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsu4345 2017-02-07 13:26
    已采纳

    Implement a __toString() method into your entity, which will return the desired name. Read more on the docs.

    Then you remove the property from the QueryBuilder, this will cause the automatic usage of the to_string() method of the class.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题