duanpi7107 2017-02-07 05: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 05: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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部