与Codeigniter集成gedmo/doctrine扩展


Integrate gedmo/doctrine extension with Codeigniter

我已经在我的新创建的项目与CodeIgniter 3.0项目与此repo和其他网站集成了doctrine form。

现在,我想使用学说扩展,gedmo,它不工作,但也没有错误,但当我试图在数据库上创建一行时,它说created_at不能为null,它具有gedmo/timestamable来获取当前日期时间。

这是我的Doctrine.php文件

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
use Doctrine'Common'ClassLoader,
    Doctrine'ORM'Configuration,
    Doctrine'ORM'EntityManager,
    Doctrine'Common'Cache'ArrayCache,
    Doctrine'Common'Annotations'AnnotationReader,
    Doctrine'ORM'Mapping'Driver'AnnotationDriver,
    Doctrine'DBAL'Logging'EchoSQLLogger,
    Doctrine'Common'EventManager;
use Gedmo'Timestampable'TimestampableListener,
    Gedmo'Sluggable'SluggableListener;
class Doctrine {
public $em = null;
public function __construct()
{
    // load database configuration from CodeIgniter
    require_once APPPATH.'config/database.php';

    //A Doctrine Autoloader is needed to load the models
    // first argument of classloader is namespace and second argument is path
    // setup models/entity namespace
    $entityLoader = new ClassLoader('models', APPPATH);
    $entityLoader->register();
    foreach (glob(APPPATH.'modules/*', GLOB_ONLYDIR) as $m) {
        $module = str_replace(APPPATH.'modules/', '', $m);
        $entityLoader = new ClassLoader($module, APPPATH.'modules');
        $entityLoader->register();
    }
    //Register proxies namespace
    $proxyLoader = new ClassLoader('Proxies', APPPATH.'Proxies');
    $proxyLoader->register();

    // Set up caches
    $config = new Configuration;
    $cache = new ArrayCache;
    $config->setMetadataCacheImpl($cache);
    $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models'));
    $config->setMetadataDriverImpl($driverImpl);
    $config->setQueryCacheImpl($cache);
    // Set up entity
    $reader = new AnnotationReader($cache);
    $models = array(APPPATH.'models');
    foreach (glob(APPPATH.'modules/*/models', GLOB_ONLYDIR) as $m)
        array_push($models, $m);
    $driver = new AnnotationDriver($reader, $models);
    $config->setMetadataDriverImpl($driver);
    // Setup Gedmo
    $cachedAnnotationReader = new Doctrine'Common'Annotations'CachedReader(
        $reader, // use reader
        $cache // and a cache driver
    );
    // create a driver chain for metadata reading
    $driverChain = new Doctrine'ORM'Mapping'Driver'DriverChain();
    // load superclass metadata mapping only, into driver chain
    // also registers Gedmo annotations.NOTE: you can personalize it
    Gedmo'DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
        $driverChain, // our metadata driver chain, to hook into
        $cachedAnnotationReader // our cached annotation reader
    );
    $event = new EventManager;
    $timestampableListener = new TimestampableListener;
    $timestampableListener->setAnnotationReader($cachedAnnotationReader);
    $event->addEventSubscriber($timestampableListener);
    $slugListener = new SluggableListener;
    $slugListener->setAnnotationReader($cachedAnnotationReader);
    $event->addEventSubscriber($slugListener);

    // Proxy configuration
    $config->setProxyDir(APPPATH.'/proxies');
    $config->setProxyNamespace('Proxies');
    // Set up logger
    // $logger = new EchoSQLLogger;
    // $config->setSQLLogger($logger);
    $config->setAutoGenerateProxyClasses( TRUE );
    // Database connection information
    $connectionOptions = array(
        'driver' => 'pdo_mysql',
        'user' =>     $db['default']['username'],
        'password' => $db['default']['password'],
        'host' =>     $db['default']['hostname'],
        'dbname' =>   $db['default']['database']
    );
    // Create EntityManager
    $this->em = EntityManager::create($connectionOptions, $config);
}

}

有谁能告诉我,我错过了什么?

我觉得你需要打电话给Gedmo !

        // Set up models loading
    $loader = new ClassLoader('models', APPPATH);
    $loader->register();
    foreach (glob(APPPATH . 'modules/*', GLOB_ONLYDIR) as $m) {
        $module = str_replace(APPPATH . 'modules/', '', $m);
        $loader = new ClassLoader($module, APPPATH . 'modules');
        $loader->register();
    }
    // Set up Gedmo
    $classLoader = new ClassLoader('Gedmo', APPPATH . 'third_party');
    $classLoader->register();
    $evm = new EventManager;
    // timestampable
    $evm->addEventSubscriber(new TimestampableListener);
    // sluggable
    $evm->addEventSubscriber(new SluggableListener);
    // tree
    $evm->addEventSubscriber(new TreeListener);
    // Set up proxies loading
    $loader = new ClassLoader('Proxies', APPPATH . 'Proxies');
    $loader->register();
    // Set up caches
    $config = new Configuration;
    $cache = new ArrayCache;
    $config->setMetadataCacheImpl($cache);
    // Set up models
    $models = array(APPPATH . 'models');
    foreach (glob(APPPATH . 'modules/*/models', GLOB_ONLYDIR) as $m)
        array_push($models, $m);
    // Set up driver
    $driverImpl = $config->newDefaultAnnotationDriver($models);
    $config->setMetadataDriverImpl($driverImpl);
    $config->setQueryCacheImpl($cache);

    // Proxy configuration
    $config->setProxyDir(APPPATH . 'Proxies'); //must be set to chmod 777
    $config->setProxyNamespace('Proxies');

我制作了这个解决方案来集成Doctrine 2 + Codeigniter 3 HMVC + composer

我使用composer安装Doctrine

"minimum-stability": "stable",
"require": {        
    "doctrine/orm": "2.4.*",
    "gedmo/doctrine-extensions": "2.4.*",
    "phpmailer/phpmailer": "5.2.8"
}
架构

-application
    --config
    --...
    --libraries
    ---Doctrine.php
    --vendor
    --proxies (must be set to 777)
    --composer.json

然后通过autolload .php文件$ autolload ['libraries'] = array('doctrine');

然后创建我的Doctrine.php文件,如下所示:

use Doctrine'Common'ClassLoader,
    Doctrine'ORM'Configuration,
    Doctrine'ORM'EntityManager,
    Doctrine'Common'Cache'ArrayCache,
    Doctrine'DBAL'Logging'EchoSQLLogger,
    Doctrine'DBAL'Event'Listeners'MysqlSessionInit,
    Doctrine'ORM'Tools'SchemaTool,
    Doctrine'Common'EventManager,
    Doctrine'Common'Annotations'AnnotationReader,
    Doctrine'ORM'Mapping'Driver'AnnotationDriver,
    Gedmo'Timestampable'TimestampableListener,
    Gedmo'Sluggable'SluggableListener,
    Gedmo'Tree'TreeListener
    ;
class Doctrine {
public $em = null;
public $tool = null;
public function __construct()
{
    // Is the config file in the environment folder?
    if (!defined('ENVIRONMENT') OR !file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/database.php')) {
        $file_path = APPPATH.'config/database.php';
    }
    // load database configuration from CodeIgniter
    require $file_path;

    // Set up class loading. You could use different autoloaders, provided by your favorite framework,
    // if you want to.
    require_once APPPATH.'vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php';
    $doctrineClassLoader = new ClassLoader('Doctrine',  APPPATH.'libraries');
    $doctrineClassLoader->register();
    $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
    $entitiesClassLoader->register();
    $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'proxies');
    $proxiesClassLoader->register();

    foreach (glob(APPPATH . 'modules/*', GLOB_ONLYDIR) as $m) {
        $module = str_replace(APPPATH . 'modules/', '', $m);
        $loader = new ClassLoader($module, APPPATH . 'modules');
        $loader->register();
    }

    $evm = new EventManager;
    // timestampable
    $evm->addEventSubscriber(new TimestampableListener);
    // sluggable
    $evm->addEventSubscriber(new SluggableListener);
    // tree
    $evm->addEventSubscriber(new TreeListener);

    // Set up caches
    $config = new Configuration;
    $cache = new ArrayCache;
    $config->setMetadataCacheImpl($cache);
    $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
    $config->setMetadataDriverImpl($driverImpl);
    $config->setQueryCacheImpl($cache);
    $config->setQueryCacheImpl($cache);
    // Proxy configuration
    $config->setProxyDir(APPPATH.'/proxies'); //must be set to 777
    $config->setProxyNamespace('Proxies');
    // Set up logger
    $logger = new EchoSQLLogger;
    $config->setSQLLogger($logger);

    if (ENVIRONMENT == "development") {
        $config->setAutoGenerateProxyClasses( TRUE );
    } else {
        $config->setAutoGenerateProxyClasses( FALSE );
    }

    // Database connection information
    $connectionOptions = array(
        'driver' => 'pdo_mysql',
        'user' =>     $db[$active_group]['username'],
        'password' => $db[$active_group]['password'],
        'host' =>     $db[$active_group]['hostname'],
        'dbname' =>   $db[$active_group]['database']
    );
    // Create EntityManager
    $this->em = EntityManager::create($connectionOptions, $config);

    // Force UTF-8
    $this->em->getEventManager()->addEventSubscriber( new MysqlSessionInit('utf8', 'utf8_unicode_ci'));
    // Schema Tool
    $this->tool = new SchemaTool($this->em);
}
}

和它的工作,希望这将帮助,:)