Mitchell McKenna - tagged with doctrine http://mitchmckenna.com/feed en-us http://blogs.law.harvard.edu/tech/rss LifePress mitchellmckenna@gmail.com Laravel Queued Jobs + Doctrine - Fix Objects Not Updating Between Jobs Run http://mitchmckenna.com/post/15632/laravel-queued-jobs-doctrine-fix-objects-not-updating-between-jobs-run

The issue is that doctrine is keeping the objects in local memory because the daemon is basically a php long-running process. In order to allow the objects used by the job to get updated, you need to $em->detach() the objects. But you'd also have to have cacade={"detach"} on all relationships that object has as well. So the best thing is to just call $em->clear() at the end of your job. This will detach all objects from doctrine and it will get a fresh copy of all the objects from the database for the next job run.

]]>
Wed, 10 Feb 2016 07:34:54 -0500 http://mitchmckenna.com/post/15632/laravel-queued-jobs-doctrine-fix-objects-not-updating-between-jobs-run/laravel-queued-jobs-doctrine-fix-objects-not-updating-between-jobs-run
Laravel Queued Jobs + Doctrine - Fix Mysql Has Gone Away Errors http://mitchmckenna.com/post/15630/laravel-queued-jobs-doctrine-fix-mysql-has-gone-away-errors

With long running processes you can get a 2006 MySQL server has gone away error. This adds a class that calls Doctrine's DBAL Connection's $conn->ping() to see if it's gone away, if so close the connection and let DBAL automatically open a new connection on the next query attempt. This is talked about in the Doctrine's Github repo here. Here's some pseudo code on how to do it:

<?php

namespace Acme\Doctrine\ORM;

use Doctrine\ORM\EntityManager;

class ConnectionSaver
{
    /**
     * Ping the DB server, if connection is lost close it and DBAL will
     * automatically reconnect on the next DB call.
     */
    public function checkDatabaseConnection()
    {
        /** @var \Doctrine\DBAL\Connection */
        $conn = app(EntityManager::class)->getConnection();

        try {
            $ping = $conn->ping();
        } catch (\ErrorException $e) {
            $conn->close();
        }
    }
}

Then inside the command handler you just inject the class and call checkDatabaseConnection() function.

<?php

namespace App\Listeners\Commands;

use Acme\Doctrine\ORM\ConnectionSaver;

class FooHandler
{
    public function __construct(ConnectionSaver $connectionSaver) {
        $connectionSaver->checkDatabaseConnection();
    }
]]>
Wed, 20 Jan 2016 03:34:54 -0500 http://mitchmckenna.com/post/15630/laravel-queued-jobs-doctrine-fix-mysql-has-gone-away-errors/laravel-queued-jobs-doctrine-fix-mysql-has-gone-away-errors
orms were first created by Richard Stallman, what did you think the "RMS" stood http://mitchmckenna.com/post/15905/mitchellmckenna-orms-were-first-created-by-richard-stallman-what-did-you-think-the-rms-stood-for-doctrine-eloquent-propel

#orms were first created by Richard Stallman, what did you think the "RMS" stood for? #doctrine #eloquent #propel

]]>
Thu, 29 Oct 2015 13:13:00 -0400 http://mitchmckenna.com/post/15905/mitchellmckenna-orms-were-first-created-by-richard-stallman-what-did-you-think-the-rms-stood-for-doctrine-eloquent-propel/mitchellmckenna-orms-were-first-created-by-richard-stallman-what-did-you-think-the-rms-stood-for-doctrine-eloquent-propel
Marco Pivetta (Ocramius) - Doctrine ORM Good Practices and Tricks http://mitchmckenna.com/post/15802/phpday-2015-marco-pivetta-doctrine-orm-good-practices-and-tricks

Interesting presentation from one of the Doctrine core team members.

In this Talk, we are going to dive into Doctrine 2 ORM’s feature set. We’re going to see the advantages and disadvantages of each functionality, with a particular focus on use-cases that each functionality attempts to solve, which features should be used with care (or avoided completely) and which ones should be used even more.

]]>
Tue, 29 Sep 2015 17:39:00 -0400 http://mitchmckenna.com/post/15802/phpday-2015-marco-pivetta-doctrine-orm-good-practices-and-tricks/phpday-2015-marco-pivetta-doctrine-orm-good-practices-and-tricks
Laravel Doctrine - A drop-in Doctrine2 implementation for Laravel 5 http://mitchmckenna.com/post/15757/laravel-doctrine-a-drop-in-doctrine2-implementation-for-laravel-5

Several php packages to easily integrate Doctrine 2 into your next Laravel/Lumen project. The 'orm' package includes a ServiceProvider to add Doctrine to your project and configure it. The 'extensions' package includes several extensions including timestampable, softdeletable, etc. The 'migrations' package add ability to use Doctrine's migration package instead of laravel's migrations so that you can easily take advantage of auto migration generation based on entities. The 'ACL' package includes traits to add roles and permissions to user objects to easily integrate with Laravel's new ACL.

]]>
Mon, 14 Sep 2015 14:22:00 -0400 http://mitchmckenna.com/post/15757/laravel-doctrine-a-drop-in-doctrine2-implementation-for-laravel-5/laravel-doctrine-a-drop-in-doctrine2-implementation-for-laravel-5
Doctrine cli-config.php for Laravel 5 http://mitchmckenna.com/post/15205/doctrine-cli-configphp-for-laravel-5

Doctine requires a custom made cli-config.php file for Setting up the Commandline Tool with a Laravel 5 application. The CLI tool is necessary when you need to for example clear Doctrine Cache using php vendor/bin/doctrine orm:clear-cache:metadata.

The following is what you can put in your cli-config.php:

<?php

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap
/** @var Illuminate\Foundation\Application $app */
$app = require_once __DIR__ . '/bootstrap/app.php';

/** @var Illuminate\Contracts\Console\Kernel $kernel */
$kernel = $app->make('Illuminate\Contracts\Console\Kernel');
$kernel->bootstrap();

$app->boot();

// replace with mechanism to retrieve EntityManager in your app
$entityManager = $app[EntityManager::class];

return ConsoleRunner::createHelperSet($entityManager);


Note you will need to have already registered the EntityManager with your app through a service provider. You can use a package like mitchellvanw/laravel-doctrine or opensolutions/doctrine2bridge-l5 to setup Doctrine in Laravel 5.

]]>
Tue, 24 Feb 2015 02:53:35 -0500 http://mitchmckenna.com/post/15205/doctrine-cli-configphp-for-laravel-5/doctrine-cli-configphp-for-laravel-5