Symfony2 Data Transformer收到null

时间:2015-06-10 11:26:02

标签: php forms symfony

我的数据转换器在所有情况下都会收到空值,但是'multiple'=>true

现在我只想尝试var_dump值。

这是我的表格:

$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))

这是我的变压器:     

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;
}

知道为什么会发生这种情况以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果你在这里阅读

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

转换应该将对象更改为字符串(实体为其id),在文档中尝试它

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

    return $value->getId();
}

另外,为什么需要实体类型字段的变换器?