Source for file DoctrineContainer.php
Documentation is available at DoctrineContainer.php
namespace Wildkat\ YiiExt\ DoctrineOrm;
Doctrine\ Common\ ClassLoader,
Doctrine\ Common\ Annotations\ AnnotationReader,
Doctrine\ ORM\ Mapping\ Driver\ AnnotationDriver;
require_once 'vendor/Doctrine/Common/ClassLoader.php';
* This Yii application component acts as a container for the Doctrine 2 ORM
* library. Firstly, you will need to download the latest version of the Doctrine 2 ORM
* library and place it inside ext.Wildkat.YiiExt.DoctrineOrm.vendors. The directory
* layout should look like the following;
* Now, inside main.php config file, set up the component with 3 keys
* (dbal, entityManager & cache) where each key represents a configuration
* set for each D2 component. For example;
* [multiple dbal configurations here]
* 'entityManager' => array(
* [multiple entity manager configurations here]
* [multiple cache configurations here]
* You must also set and alias for the Wildkat namespace. At the top of the main.php
* configuration file, put
* Yii::setPathOfAlias('Wildkat', realpath(dirname(__FILE__) . '/../extensions/Wildkat'));
* For each D2 component, you can specify any number of configurations and index
* them numerically or with a string. You can then access each configuration
* through the component method e.g. Yii::app()->doctrine->getConnection('foo')
* returns a dbal connection with a configuration index of 'foo'.
* @category YiiExtensions
* @package Wildkat\YiiExt\DoctrineOrm
* @author Kevin Bradwick <kevin@wildk.at>
* @license New BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: ##VERSION##
* @link http://www.wildk.at
* List of dbal configurations. Each configuration has the following options
* @link http://www.doctrine-project.org/docs/dbal/2.0/en/reference/configuration.html
* See the Doctrine Documentation for an explanation of connection parameters
* List of cache configurations. Each with the following options
* <li>driver - (string) the driver e.g. ApcCache, MemcacheCache</li>
* <li>namespace - (string) the cache namespace</li>
* <li>servers - (array) used for memcache</li>
* List of entity manager configurations. Each with the following options
* <li>connection - (string) the dbal config name</li>
* <li>mappingDriver - (string) [AnnotationDriver, YamlDriver, XmlDriver]</li>
* <li>mappingPaths - (array) An array of paths to find mapping information</li>
* <li>mappingDriverOptions - (array) Additional mapping driver options
* defined in and array and make reference to each of the drivers set
* <li>metadataCache - (string) the cache configuration for metadata</li>
* <li>queryCache - (string) the cache configuration for query conversions</li>
* <li>resultCache - (string) the cache configuration for results</li>
* <li>proxyDir - (string)the directory location for proxy classes</li>
* <li>proxyNamespace - (string) the proxy namespace</li>
* <li>entityNamespaces - (array) entity namespaces</li>
* <li>autoGenerateProxyClasses - (bool) true false</li>
* Cached component instances
* Component initialization.
* This method registers doctrine's autoloaders by pushing them on the
* current autoloader stack.
* @see CApplicationComponent::init()
$classLoader = new ClassLoader('Doctrine', dirname(__FILE__ ) . '/vendor');
\ Yii::registerAutoloader(array($classLoader, 'loadClass'));
$classLoader = new ClassLoader('Symfony', dirname(__FILE__ ) . '/vendor');
\ Yii::registerAutoloader(array($classLoader, 'loadClass'));
* Get an instance of a DBAL Connection
* @param sting $name the connection name
* @return Doctrine\DBAL\Connection
if (isset ($this->_cache['dbal'][$name]) === true
&& $this->_cache['dbal'][$name] instanceof DBAL\ Connection
return $this->_cache['dbal'][$name];
$config = $this->dbal[$name];
$conn = DBAL\ DriverManager::getConnection(
$this->_getDBALConfiguration($config),
$this->_getEventManager($config)
$this->_cache['dbal'][$name] = $conn;
* Returns a cache instance. Drivers can be specified by their
* fully qualified name e.g. Doctrine\Common\Cache\ArrayCache or by their
* short name e.g. ArrayCache.
* If driver class is a custom implementation, it must extend from
* Doctrine\Common\Cache\AbstractCache.
* @param sting $name the cache name
* @return Doctrine\Common\Cache\AbstractCache
public function getCache($name= 'default')
if (isset ($this->_cache['cache'][$name]) === true
&& $this->_cache['cache'][$name] instanceof Cache\ AbstractCache
return $this->_cache['cache'][$name];
if (isset ($this->cache[$name]) === false) {
'Unknown cache configuration "{name}"',
$doctrineDrivers = array(
'ApcCache' => 'Doctrine\Common\Cache\ApcCache',
'ArrayCache' => 'Doctrine\Common\Cache\ArrayCache',
'MemcacheCache' => 'Doctrine\Common\Cache\MemcacheCache',
'XcacheCache' => 'Doctrine\Common\Cache\XcacheCache',
$config = $this->cache[$name];
$driver = new $doctrineDrivers[$config['driver']];
} else if (in_array($config['driver'], $doctrineDrivers) === true) {
$driver = new $config['driver'];
} else if (isset ($config['driver']) === true) {
$driver = new $config['driver'];
if ($driver instanceof Cache\ AbstractCache === false) {
'Cache driver must inherit from AbstractCache'
if (isset ($driver) === false) {
'Unknown cache configuration "{name}"',
if (isset ($config['namespace']) === true) {
$driver->setNamespace($config['namespace']);
$driver->initialize($config);
if ($driver instanceof Cache\ MemcacheCache) {
$memcache = new \ Memcache();
if (isset ($config['servers']) === true) {
foreach ($config['servers'] as $server) {
$server['retryInterval'],
$defaultServer['persistent'],
$defaultServer['weight'],
$defaultServer['timeout'],
$defaultServer['retryInterval'],
$driver->setMemcache($memcache);
$this->_cache['cache'][$name] = $driver;
* Returns an entity manager
* @param string $name the entity manager configuration name
* @return Doctrine\ORM\EntityManager
if (isset ($this->_cache['em'][$name]) === true) {
return $this->_cache['em'][$name];
'Unknown entity manager configuration "{name}"',
$config = new ORM\ Configuration();
unset ($options['connection']);
$driver = $this->_getMappingDriver($options);
$config->setMetadataDriverImpl($driver);
if (isset ($options['metadataCache']) === true) {
$config->setMetadataCacheImpl(
$this->getCache($options['metadataCache'])
unset ($options['metadataCache']);
if (isset ($options['queryCache']) === true) {
$config->setQueryCacheImpl(
unset ($options['queryCache']);
if (isset ($options['resultCache']) === true) {
$config->setResultCacheImpl(
$this->getCache($options['resultCache'])
unset ($options['resultCache']);
$options['proxyDir'] = \ Yii::getPathOfAlias($options['proxyDir']);
// loop through setters of remaining options
foreach ($options as $key => $value) {
$config->{$method}($value);
$em = ORM\ EntityManager::create($conn, $config);
$this->_cache['em'][$name] = $em;
}//end getEntityManager()
* Take the configuration and return a mapping driver
* @param array &$config the driver options
* @return Doctrine\ORM\Mapping\Driver\Driver
private function _getMappingDriver(array & $config)
'XmlDriver' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
'YamlDriver' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
if (is_array($config['mappingPaths']) === true) {
foreach ($config['mappingPaths'] as $index => $path) {
$config['mappingPaths'][$index] = \ Yii::getPathOfAlias($path);
$config['mappingPaths'] = \ Yii::getPathOfAlias($config['mappingPaths']);
// set default annotation driver
|| $config['mappingDriver'] === 'AnnotationDriver'
$reader = new AnnotationReader();
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
$driver = new AnnotationDriver($reader, $config['mappingPaths']);
unset ($config['mappingDriver']);
unset ($config['mappingPaths']);
$mappingClass = $drivers[$config['mappingDriver']];
$driver = new $mappingClass($config['mappingPaths']);
if (isset ($config['mappingDriverOptions']) === true) {
foreach ($config['mappingDriverOptions'] as $key => $value) {
$driver->{$method}($value);
unset ($config['mappingDriverOptions']);
unset ($config['mappingDriver']);
unset ($config['mappingDriverPaths']);
}//end _getMappingDriver()
* Get an event manager configuration
* @param array $config the configuration
* @return Doctrine\Common\EventManager|null
private function _getEventManager(array $config= array())
if (isset ($config['eventManagerClass']) === false) {
if (isset ($this->_cache['eventManager']) === false) {
$eventManagerClass = $config['eventManagerClass'];
$this->_cache['eventManager'] = new $eventManagerClass();
if (isset ($config['eventSubscribers']) === true) {
foreach ($config['eventSubscribers'] as $subscriber) {
$sub = new $subscriber();
$this->_cache['eventManager']->addEventSubscriber($sub);
return $this->_cache['eventManager'];
}//end _getEventManager()
* Get a DBAL configuration
* @param array $config the configuration
* @return Doctrine\DBAL\Configuration|null
private function _getDBALConfiguration(array $config= array())
$configClass = $config['configurationClass'];
$configuration = new $configClass();
if (empty($config['sqlLoggerClass']) === false) {
$sqlLoggerClass = $config['sqlLoggerClass'];
$loggerClass = new $sqlLoggerClass();
$configuration->setSQLLogger($loggerClass);
}//end _getDBALConfiguration()
|