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.

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

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面