简体   繁体   English

Symfony2数据转换器收到空

[英]Symfony2 Data Transformer receives null

My data transformer receives null values in all cases but those with 'multiple'=>true . 我的数据转换器在所有情况下都接收空值,但'multiple'=>true

For now I'm just trying to var_dump the values. 现在,我只是想将var_dump的值。

This is my form: 这是我的表格:

$entitiesClasses = array(
        'AppBundle\Entity\Category' => array( 'label' => 'entity.vehicle.category' ),
        'AppBundle\Entity\Specific\ExteriorColor' => array( 'label' => 'entity.specific.exteriorColor' ),
        'AppBundle\Entity\Specific\DoorCount' => array( 'label' => 'entity.specific.doorCount' ),
        'AppBundle\Entity\Specific\Fuel' => array( 'label' => 'entity.specific.fuel' ),
        'AppBundle\Entity\Specific\Gearbox' => array( 'label' => 'entity.specific.gearbox' ),
        'AppBundle\Entity\Specific\Climatisation' => array( 'label' => 'entity.specific.climatisation' ),
        'AppBundle\Entity\Specific\WheelFormula' => array( 'label' => 'entity.specific.wheelFormula' ),
        'AppBundle\Entity\Specific\InteriorColor' => array( 'label' => 'entity.specific.interiorColor' ),
        'AppBundle\Entity\Specific\InteriorType' => array( 'label' => 'entity.specific.interiorType' ),
        'AppBundle\Entity\Specific\ParkingAssistant' => array(  'label' => 'entity.specific.parkingAssistant', 'multiple' => true ),
        'AppBundle\Entity\Feature' => array(  'label' => 'entity.vehicle.features', 'multiple' => true ),
    );

    $getEntityInputName = function($className) {
        $test = preg_split('/\\\/', $className);
        return lcfirst(end($test));
    };

    foreach($entitiesClasses as $className => $options) {
        $inputName = $getEntityInputName($className);
        isset($options['multiple']) && $options['multiple'] == true? $inputName .= 's' : null;

        $formMapper->add(
            $formMapper->getFormBuilder()->create($inputName, 'entity',
                array_merge(
                    $options,
                    array(
                        'required' => false,
                        'class' => $className,
                        'query_builder' => $query_builder,
                    ))
            )->addModelTransformer(new EntityTranslationTransformer($this->em))

This is my transformer: 这是我的变压器:

namespace AppBundle\Form\DataTransformer;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;


class EntityTranslationTransformer implements DataTransformerInterface
{
/**
 * @var EntityManager
 */
private $em;

/**
* @var mixed $entity
*/
private $entity;

/**
 * @param EntityManager $em
 * @param string $entity
 */
public function __construct(EntityManager $em)
{
    $this->em = $em;
}

public function transform($value)
{
    if ($value === null) {
        return null;
    }

    return $value;
}

Any idea why this is happening and how to fix it? 知道为什么会这样以及如何解决吗?

if you read here 如果你在这里读

http://symfony.com/doc/current/cookbook/form/data_transformers.html#creating-the-transformer http://symfony.com/doc/current/cookbook/form/data_transformers.html#creating-the-transformer

transform should change an object into string(an entity to its id), try it as it is in the docs 转换应将对象更改为字符串(其ID为实体),请按文档中的说明尝试

    public function transform($value)
{
    if (null === $value) {
        return "";
    }

    return $value->getId();
}

In addition, why do you need a transformer for an entity type field? 另外,为什么您需要一个用于实体类型字段的转换器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM