Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Sonata admin datatransformer

I'm trying to set a form type "sonata_type_immutable_array" as follows:

->add('metadatos', 'sonata_type_immutable_array', array(
    'keys' => array(
                    array('Test', 'text', array('required' => false)),
                    array('URL', 'url',  array('required' => false)),
                )
            ))

And saving like this:

public function setMetadatos(\Portal\EntradasBundle\Entity\EntradaMeta $metadatos = null)
{
    $this->metadatos = $metadatos;

    return $this;
}

But always get the error:

Catchable Fatal Error: Argument 1 passed to Portal\EntradasBundle\Entity\Entrada::setMetadatos() must be an instance of Portal\EntradasBundle\Entity\EntradaMeta, array given

I dont know how to set a datatransformer (ArrayToModelTransformer) to reach this.

Anyone can help me plz. Thanks in advance!

like image 628
user1795760 Avatar asked Nov 12 '22 18:11

user1795760


1 Answers

A data transformer is quite simple, Look at this: http://symfony.com/doc/current/cookbook/form/data_transformers.html

A data transformer is used like this:

    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @param ObjectManager $om
     */
    public function __construct($om)
    {
        $this->om = $om;
    }

[..]
$yourTransformer = new YourDataTransformer($this->om);

And then ->addModelTransformer($yourTransformer))

It's used to get the id of an object , and/or get the object from an id.

like image 150
Skizomik Avatar answered Nov 15 '22 10:11

Skizomik