2015-10-19 8 views

Odpowiedz

4

Install Nauki

Doctrine 2 ORM’s documentation - Installation and Configuration

Doktryna może być instalowany z Composer. Zdefiniuj następujące wymagania w pliku composer.json:

{ 
    "require": { 
     "doctrine/orm": "*" 
    } 
} 

Następnie zadzwonić kompozytor instalacji z linii poleceń.

Integracja z CodeIgniter

Doctrine 2 ORM’s documentation - Integrating with CodeIgniter

Oto kroki: Dodaj plik php do systemu/aplikacji/bibliotek folder o nazwie Doctrine.php. To będzie twój wrapper/bootstrap dla menedżera encji D2. Umieść folder Doctrine (ten, który zawiera Common, DBAL i ORM) wewnątrz folderu third_party. Jeśli chcesz otworzyć swój plik konfiguracyjny/autoload.php i automatycznego ładowania biblioteki nauką $autoload[‘libraries’] = array(‘doctrine’);

Tworzenie biblioteki Doctrine CodeIgniter

Teraz tutaj jest to, co plik Doctrine.php powinien wyglądać. Dostosuj go do swoich potrzeb.

<?php 
/** 
* Doctrine 2.4 bootstrap 
* 
*/ 

use Doctrine\Common\ClassLoader, 
    Doctrine\ORM\Configuration, 
    Doctrine\ORM\EntityManager, 
    Doctrine\Common\Cache\ArrayCache, 
    Doctrine\DBAL\Logging\EchoSQLLogger; 


class Doctrine { 

    public $em = null; 

    public function __construct() 
    { 
    // load database configuration from CodeIgniter 
    require_once APPPATH.'config/database.php'; 

    // include Doctrine's ClassLoader class 
    require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php'; 

    // load the Doctrine classes   
    $doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'third_party'); 
    $doctrineClassLoader->register(); 
    // load the entities 
    $entityClassLoader = new ClassLoader('Entities', APPPATH.'models'); 
    $entityClassLoader->register(); 
    // load the proxy entities 
    $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies'); 
    $proxiesClassLoader->register(); 
    // load Symfony2 classes 
    // this is necessary for YAML mapping files and for Command Line Interface (cli-doctrine.php) 
    $symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine'); 
    $symfonyClassLoader->register(); 

    // Set up the configuration 
    $config = new Configuration; 

    // Set up caches 
    if(ENVIRONMENT == 'development') // set environment in index.php 
     // set up simple array caching for development mode 
     $cache = new \Doctrine\Common\Cache\ArrayCache; 
    else 
     // set up caching with APC for production mode 
     $cache = new \Doctrine\Common\Cache\ApcCache; 
    $config->setMetadataCacheImpl($cache); 
    $config->setQueryCacheImpl($cache); 

    // set up annotation driver 
    $driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings'); 
    $config->setMetadataDriverImpl($driver); 

    // Proxy configuration 
    $config->setProxyDir(APPPATH.'/models/Proxies'); 
    $config->setProxyNamespace('Proxies'); 

    // Set up logger 
    $logger = new EchoSQLLogger; 
    $config->setSQLLogger($logger); 

    $config->setAutoGenerateProxyClasses(TRUE); // only for development 

    // 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, and store it for use in our CodeIgniter controllers 
    $this->em = EntityManager::create($connectionOptions, $config); 
    } 
} 

Konfigurowanie narzędzie wiersza polecenia

Doktryna statków z wielu narzędzi wiersza polecenia, które są bardzo pomocne w trakcie rozwoju.

Sprawdź, czy linie te istnieje w pliku Doctrine.php, aby załadować klasy Symfony za pomocą narzędzi wiersza poleceń (i plików mapowania YAML):

$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine'); 
$symfonyClassLoader->register(); 

Musisz zarejestrować swoje aplikacje do EntityManager narzędzie konsoli do korzystania z zadań, tworząc plik CLI-doctrine.php w katalogu aplikacji o następującej treści:

<?php 

/** 
* Doctrine CLI bootstrap for CodeIgniter 
* 
*/ 

define('APPPATH', dirname(__FILE__) . '/'); 
define('BASEPATH', APPPATH . '/../system/'); 
define('ENVIRONMENT', 'development'); 

require APPPATH.'libraries/Doctrine.php'; 

$doctrine = new Doctrine; 
$em = $doctrine->em; 

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) 
)); 

\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet); 

?> 

teraz uruchomić ten skrypt za pomocą wiersza polecenia PHP i powinien zobaczyć listę poleceń Dostępny dla Ciebie.

php cli-doctrine.php 

Generowanie klas odwzorowań z bazy danych:

php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities 

jeśli pojawi się ten błąd: Błąd krytyczny: Zadzwoń do funkcji niezdefiniowany Doctrine \ Common \ Cache \ apc_fetch() zainstalować rozszerzenie APC dla PHP:

sudo apt-get install php-apc 
sudo /etc/init.d/apache2 restart 

Dla tryb produkcji będziemy chcieli korzystać z systemu rzeczywistego buforowania jak APC, pozbyć EchoSqlLogger i wyłącz autoGenerateProxyClasses w Doctrine.php

+0

Wykonaj następujące kroki, a nie działa ... Doctrine 2.4 i CodeIgniter 3.1.4. – omixam

1

Na Cl 3 + HMVC + Nauki 2,4

<?php 

use Doctrine\Common\Annotations\AnnotationReader; 
use Doctrine\Common\Cache\ArrayCache; 
use Doctrine\Common\ClassLoader; 
use Doctrine\Common\EventManager; 
use Doctrine\DBAL\Event\Listeners\MysqlSessionInit; 
use Doctrine\DBAL\Logging\EchoSQLLogger; 
use Doctrine\ORM\Configuration; 
use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; 
use Doctrine\ORM\Tools\SchemaTool; 
use Gedmo\Sluggable\SluggableListener; 
use Gedmo\Timestampable\TimestampableListener; 
use 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); 

    } 
}