简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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