指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

symfony之persist和flush的区别:

The main difference between persist() and persistFlush() is that persist() queues the entity for insertion into the database, but does not immediately execute the insert statement. Instead, the insert is executed when the transaction is committed or when the flush() method is called.

persistFlush(), on the other hand, combines the persist() and flush() methods into a single call. It queues the entity for insertion and immediately executes the insert statement, flushing the changes to the database.

Both persist() and persistFlush() can be useful in different situations. persist() can be useful when you want to queue multiple entities for insertion and flush them all to the database at once, while persistFlush() can be useful when you want to immediately persist an entity and ensure that it is saved to the database.

In general, it's a good idea to use persist() when you want to optimize for performance and persistFlush() when you want to ensure that the entity is immediately persisted to the database.

上面的文章翻译过来就是:persist会先把插入数据库操作放到队列里面,然后通过flush一起持久化到数据库,flush会立即持久化到数据库

使用案列:

    public function updateVideo($params = []): array
    {
        $dateTime = TimeService::getDateTime(time());
        try {
            $this->getEntityManager()->getConnection()->transactional(function () use ($params, $dateTime) {
                $data = $this->find($params['id']);
                $data->setTitle($params['title']);
                $data->setLength($params['length']);
                $data->setPlayUrl($params['playUrl']);
                $data->setUpdatedAt($dateTime);

                 //①
                //$this->save($data, true);

                $cm = $this->getEntityManager()->getRepository(CourseManage::class)->findOneBy(['type' => 'video', 'contentId' => $params['id'], 'courseId' => $data->getCourseId()]);
                if ($cm) {
                    $cm->setTitle($params['title']);
                    $cm->setUpdatedAt($dateTime);

                    //②
                    //$this->getEntityManager()->getRepository(CourseManage::class)->save($cm, true);
                }

                //③
                $this->getEntityManager()->flush();
            });
        } catch (\Throwable $exception) {
            return ['status' => false, 'msg' => $exception->getMessage()];
        }

        return ['status' => true, 'msg' => 'operate success'];
    }

上面代码分析:当①和② 都设置为true的时候,就会自动调用flush,当没有设置true的时候就只是persist到队列,没有flush,这个时候需要 手动调用$this->getEntityManager()->flush() 即可。同时如果没有①和②,直接调用$this->getEntityManager()->flush(),也是可以 把数据持久化到数据库的。

参考文档: https://www.doctrine-project.org/projects/doctrine1/en/latest/manual/component-overview.html