V
V
v7sila2018-08-30 11:38:04
symfony
v7sila, 2018-08-30 11:38:04

How to create multiple records in a table with doctrine in one query?

In sql, to add multiple records in one query, you can do this:

insert into tablename (id,blabla) values(1,'werwer'),(2,'wqewqe'),(3,'qwewe');

In my case, I did this:
public function addImages($idService, $newFilePath, $userId){
        // Подключение менеджера сущьностей Doctrine
        $em = $this->managerInterface;
        // Запись изображений в таблицу в таблицу
        foreach ($newFilePath as $key=>$value){
            // Создание экземпляра сущьности
            $images = new ServiceImages();

            $images->setIdService($idService);
            $images->setPathImages($value['PATH']);
            $images->setNameImages($value['NAME']);
            $images->setIdUser($userId);
            $em->persist($images);
            // Запись
        }
        
     $em->flush();

    }

But this is too many queries to the database. How to optimize?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-08-30
@v7sila

No way.
Of course, you can write a "raw" sql query bypassing doctrine, or even do LOAD DATA INFILE if there is really a lot of data. But these are already features that are implemented differently in different DBMS and the doctrine does not deal with this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question