Symfony2: How to use INSERT DELAYED with a doctrine or create a non-blocking database operation?

For performance reasons, I would like to save the log object using the mysql INSERT DELAYED query.

Do you have any ideas on how this can be done through doctrine?

+3
source share
1 answer

Why you probably shouldn't use INSERT DELAYED:

Starting with MySQL 5.6.6, INSERT DELAYED is deprecated and will be removed in a future release. Use INSERT (without DELAYED) instead.

( official documentation )

Symfony2 solution:

symfony2 , / kernel.terminate .

, . , .

:

namespace Acme\Your;

use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpKernel\Event\KernelEvent;

class LongOperationLogger
{
    protected $om;
    protected $data;

    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    public function setData($data)
    {
        $this->data = $data;
    }

    public function onKernelTerminate(KernelEvent $event)
    {
        // don't do anything if there is not data
        if ( null !== $this->data ) {
            return;
        }

        $logEntry = new LogEntry('I will not block the response.', $this->data);

        $this->om->persist($logEntry);
        $this->om->flush();
    }
}

-:

# app/config/config.yml

services:
    long_operation.logger:
        class: Acme\Your\LongOperationLogger
        tags:
            - { name: kernel.event_listener, event: kernel.terminate }
        arguments: [ "@doctrine.orm.entity_manager" ]

, - , , , .

public function someAction()
{
    // some condition
    // if (...) {
    //     ...
    // }

    $this->get('long_operation.logger')->setData($whatever)
}
+5

All Articles