Doctrine

36 views
Skip to first unread message

Verges

unread,
Dec 9, 2011, 12:09:12 PM12/9/11
to Alloy
Im a noob in the level of understanding the benefits of a framework
and in the quest for the perfect one for me, i have tested so many,
but wasn't till i try Alloy that i really felt that the quest was
over. This is really a good!, you don't need to be a php guru to
understand all the parts and how it works and the little documentation
is enough to get started. Also the plugin idea is perfect.

Now, i want to use Alloy with Doctrine, so i'll create a plugin, but
Im not sure if i should register the Doctrine library using the Alloy
autoload or the one Doctrines has, or both.
I would love some help on this.

Vance Lucas

unread,
Dec 9, 2011, 4:20:47 PM12/9/11
to allo...@googlegroups.com
I'm glad to hear that you like Alloy and found it both well designed and simple to understand. That was the goal, and I am glad others enjoy it as much as I do.

As for where to register the Doctrine namespace, I generally always recommend doing it in the plugin. You can register namespaces in the config with Alloy (or actually anywhere you have access to the Kernel), but doing it inside the plugin and putting all the Doctrine files within the plugin folder as well keeps the whole plugin self-contained and allows it to be copied into other Alloy projects with no additional configuration or other steps required.

Let me know if you run into any issues or need any further assistance.

--
Vance Lucas
http://vancelucas.com
Message has been deleted

Verges

unread,
Jan 6, 2012, 10:07:18 AM1/6/12
to Alloy
So i did a Doctrine Plugin for alloy, the idea is the same as all
others, this will return the entity manager object.
The only problem i had was for the location of the model classes,
since doctrine need them to be in the same namespace and alloy is a
hmvc framework.
So what i did was to use alloy loader and add the different locations
to the same namespace for the autoload and so the models can be
located inside each specific module.

<?php
namespace Plugin\Doctrine;
require 'lib/Doctrine/lib/Doctrine/ORM/Tools/Setup.php';
use Alloy, Doctrine\ORM\Tools\Setup, Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration;

class Plugin {

public $kernel;

public function __construct(Alloy\Kernel $kernel) {
$this->kernel = $kernel;

//use doctrine autoload for loading lib instead of
alloy's
Setup::registerAutoloadGit(__DIR__.'/lib/Doctrine');

//get paths of models container folders
$paths = $this->getModelsPath();

//associate all paths to a single namespace for
autoload
$this->kernel->loader()->registerNamespace('models',
$paths);

//doctrine conf >

$devMode = $this->kernel-
>config('app.mode.development');
if ($devMode) {
$cache = new \Doctrine\Common\Cache
\ArrayCache;
} else {
$cache = new \Doctrine\Common\Cache\ApcCache;
}

$config = new Configuration;
$config->setMetadataCacheImpl($cache);

//use php sytax metadata instead of annotation, it
loads less classes so i think is better
$driver = new \Doctrine\ORM\Mapping\Driver
\StaticPHPDriver($paths);

$config->setMetadataDriverImpl($driver);
$config->setQueryCacheImpl($cache);

//set root level models folder to have the proxies
folders
$config->setProxyDir($this->kernel-
>config('app.path.root').'/models/proxies');
$config->setProxyNamespace('proxies');

if ($devMode) {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}

//load conf from app file to use params
$dbConfig = $this->kernel-
>config('app.database.master');

//so the info in app not need to change
switch($dbConfig['adapter']){
case 'mysql':
default:
$dbConfig['adapter'] = 'pdo_mysql';
break;
}

$dbParams = array(
'driver' => $dbConfig['adapter'],
'dbname' => $dbConfig['database'],
'user' => $dbConfig['username'],
'password' => $dbConfig['password'],
'host' => $dbConfig['host'],
);

$em = EntityManager::create($dbParams, $config);

//add method to kernel, to return entity manager
walaaaa!
$this->kernel->addMethod('entityManager', function()
use ($em){
return $em;
});
}

/**
* Recursively walks the 'Module' folder selecting all that
have 'models'
* @return array of paths to models folders
*
* @todo cache the result on apc or something for the big
apps
*/
private function getModelsPath()
{
$path = $this->kernel->config('app.path.root');
$objects = new \RecursiveIteratorIterator(new
\RecursiveDirectoryIterator($path.'/Module'),
\RecursiveIteratorIterator::SELF_FIRST);
$paths = array();
foreach($objects as $name => $obj){
if($obj->isDir() and ($obj->getFilename() ==
'models'))
$paths[] = dirname($name);
}

//add root for having all models in root level if
desired
$paths[] = $path;
return $paths;
}
Reply all
Reply to author
Forward
0 new messages