12.28
O instalacji ZendFramework można przeczytać tutaj.
W tym tutorialu pokażę Wam jak kożystać z Doctrine w aplikacjach opartych o ZendFramework (ZF).
Aby nasz wytwór zadziałał poprawnie musimy umieścić Doctrine wraz z modelami w strukturze plików ZF.
Na potrzeby tego tutoriala przyjmuję następującą strukturę naszej aplikacji.
application/
configs/
controllers/
models/
Entities/
Proxies/
views/
script/
library/
public/
scripts/
Bootstrapper
Aby uruchomić Doctrine w ZF musimy pozwolić mu ładować potrzebne klasy. Do naszego Boodstrappera dodamy więc metodę uruchamiającą autloadery dla bibliotek i modeli Doctrine oraz konfigurującą połączenie z bazą danych i managera modeli Doctrine (EntityManager, EM).
Do naszego Bootstrapa należy dodać metodę.
tutorial/aplication/Boodstrap.php
$library_path = realpath(APPLICATION_PATH . '/../library');
require_once $library_path . '/Doctrine/Common/ClassLoader.php';
// Ustawianie autoloadera Doctrine
$classLoader = new Doctrine\Common\ClassLoader('Doctrine\ORM', $library_path);
$classLoader->register();
$classLoader = new Doctrine\Common\ClassLoader('Doctrine\DBAL', $library_path);
$classLoader->register();
$classLoader = new Doctrine\Common\ClassLoader('Doctrine\Common', $library_path);
$classLoader->register();
$classLoader = new Doctrine\Common\ClassLoader('Entities', realpath(APPLICATION_PATH . '/models'));
$classLoader->register();
$classLoader = new Doctrine\Common\ClassLoader('Proxies', realpath(APPLICATION_PATH . '/models'));
$classLoader->register();
// Konfiguracja Cache
$config = new Doctrine\ORM\Configuration;
$cache = new Doctrine\Common\Cache\ApcCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(realpath(APPLICATION_PATH . '/models/Entities')));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
// Konfiguracja Proxy
$config->setProxyDir(realpath(APPLICATION_PATH . '/models/Proxies'));
$config->setProxyNamespace('Proxies');
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
// Parametry bazy danych
$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => 'database.sqlite'
);
// Rejestracja EntityManagera Doctrine
Zend_Registry::set('em', Doctrine\ORM\EntityManager::create($connectionOptions, $config));
}
Biblioteki Doctrine
tutorial/library/Doctrine
Tutaj kopiujemy biblioteki Doctrine:
- Common
- DBAL
- ORM
Jeśli chcemy korzystać z magicznych narzędzi konsoli Doctrine kopiujemy również:
- Symfony
Struktura powinna wyglądać tak:
library/
Doctrine/
Common/
DBAL/
ORM/
Symfony/
Modele
Nasze modele lądują w katalogu tutorial/application/models/Entities. PHP musi miec prawa do zapisu katalogu tutorial/application/models/Proxies.
Skrypty dla magicznych narzędzi konsoli Doctrine
tutorial/scripts/doctrine (musi mieć prawa wykonania)
<?php
include('doctrine.php');
tutorial/scripts/doctrine.php
$cli = new \Symfony\Component\Console\Application('Doctrine Command Line Interface', Doctrine\Common\Version::VERSION);
$cli->setCatchExceptions(true);
$helperSet = $cli->getHelperSet();
foreach ($helpers as $name => $helper) {
$helperSet->set($helper, $name);
}
$cli->addCommands(array(
// DBAL Commands
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
// ORM Commands
new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),
));
$cli->run();
tutorial/scripts/doctrine-cli.php
$models_path = realpath(__DIR__ . '/../application/models');
require_once $library_path .'/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', $library_path);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', $library_path);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', $library_path);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', $library_path);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', $models_path);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', $models_path);
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = $config->newDefaultAnnotationDriver(array($models_path . "/Entities"));
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir($models_path . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => 'database.sqlite'
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$helpers = array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
);
Kiedy napiszesz o instalacji Doctrine 2, tak jak zapowiedziałeś na początku artu??
Sorry za spamerę, ale artykuł jest bardzo pomocny. Rzadko widać w necie takie tutki łączenia ZF i Doctrine 2.x
Witam. Ostatnio miałem bardzo mało czasu. Zmieniam pracę no i pewne problemy w domu. W tym miesiącu coś się w tym temacie ruszy :)
Super, to bardzo dobra wiadomość :)
Czekam z niecierpliwością :) Pozdro
W tutorial/scripts/doctrine.php , powinien byc wpis require __DIR__ . ‚/doctrine-cli.php; zamiast require __DIR__ . ‚/cli-config.php’;