Maciej Szpyra
Maciej Szpyra

Reputation: 312

Symfony2 Data Transformer receives null

My data transformer receives null values in all cases but those with 'multiple'=>true.

For now I'm just trying to var_dump the values.

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?

Upvotes: 1

Views: 971

Answers (1)

Bartłomiej Wach
Bartłomiej Wach

Reputation: 1986

if you read here

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

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

    return $value->getId();
}

In addition, why do you need a transformer for an entity type field?

Upvotes: 1

Related Questions