M
M
magary42016-07-20 17:48:18
symfony
magary4, 2016-07-20 17:48:18

How to save a field of type CollectionType as an array to the database?

Everywhere examples of saving in relative table
and I need in the same table

/**
     * @var string
     *
     * @ORM\Column(name="fields", type="json_array")
     */
    private $fields;
    public function __construct()
    {
        
        $this->fields = new \Doctrine\Common\Collections\ArrayCollection( );
    }

the form is submitted and in the fields field contains a collection of some objects MyFieldType
how to save them as json?
I tried
$obj = new \stdClass;
$obj->name = "la";
$obj->number = 3;
$entity->setFields(json_encode([$obj]));

"[{\"name\":\"la\",\"number\":3}]" was saved in the database,
but how can I save the ArrayCollection of MyFieldType objects?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-07-20
@prototype_denis

Read at your leisure
doctrine-orm.readthedocs.io/projects/doctrine-dbal...
And no transformations like $entity->setFields(json_encode([$obj])); should not be done.
The doctrine itself will do everything. $entity->setFields([$obj]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question