M
M
Maxim Torbeev2016-04-21 19:40:18
symfony
Maxim Torbeev, 2016-04-21 19:40:18

How to update/create json values ​​via doctrine in a field with type given json array?

Good day, comrades!
I ran into a problem that I couldn't solve myself.
Task: You need to push the images[] array into a field with the json array data type , in principle, the setter easily coped with text type fields:

<input type="text" name="images[image_intro_alt]" >

But here's how to shove the value that I get from the There are fields method into images[image_intro] with the file type :$this->uploadImage();
<input type="text" name="images[image_intro_alt]" >
<input type="text" name="images[image_intro_caption]">
<!-- бла бла бла -->
<input type="file" name="images[image_intro]">

This is how I populate the array.
Entity->setImages($request->request->get('images'))

/**
     * upload image content
     * @param $request
     * @return bool|string 
     */
    public function uploadImage($request){
        $destinationPath = '/web/uploads/content';
        $file = $request;
        $contentFullTextImageDir = $this->container->getParameter('kernel.root_dir') . '/../'.$destinationPath;
        if (null !== $file) {
            $fileName = $file->getClientOriginalName();
            $file->move($contentFullTextImageDir, $fileName);

            return $destinationPath.'/'.$fileName; //путь к файлу и его имя
        }
        return false;
    }

Thank you very much for your attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Niomin, 2016-04-21
@MaxTorbeev

Do you want to save the url?
The path to web/uploads/content corresponds to what in the query string? By default, upload/content?
Then it should be returned as a webpath, i.e. /uploads/content.
Well, and, accordingly, then something in the spirit

$images = $request->request->get('images');
$images['image_intro'] = $service->uploadImage($request);
$entity->setImages($images);
$em->flush();

Although in general I don't understand why you don't use forms .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question