User Tools

Site Tools


php:zend_1_doctrine_2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

php:zend_1_doctrine_2 [2013/03/16 17:40] (current)
Line 1: Line 1:
 +==== HOWTO integrate Doctrine 2.X with Zend Framework 1.X ====
 +09.2012
 +
 +
 +This short tutorial assumes that you already have downloaded Doctrine 2.X archive (with a name similar to DoctrineORM-2.2.2) and you have a Zend project in place.\\ ​
 +Also, we're gonna use Doctrine in combination with mysql server, but of course, this might be anything you really want (PostgreSQL,​ Oracle, etc).
 +
 +
 +=== Prepare the files for Doctrine ​ ===
 +
 +Unpack the Doctrine archive and copy the //​Doctrine//​ folder into //library// folder from the zend project
 +
 +=== Modify the configuration file === 
 +
 +<​code|application/​configs/​application.ini>​
 +...
 +doctrine.conn.host = '​127.0.0.1'​
 +doctrine.conn.user = '​root'​
 +doctrine.conn.pass = '​your_password'​
 +doctrine.conn.driv = '​pdo_mysql'​
 +doctrine.conn.dbname = '​database_name'​
 +doctrine.path.models = APPLICATION_PATH "/​models"​
 +...
 +</​code>​
 +
 +=== Prepare the bootstrapping process === 
 +
 +This is where the fun begins. The file might be longer in your project, but I will point out only the parts of interest here (some of them might be already in place in your bootstrap file). \\ 
 +The file is pretty straightforward (even if it looks scary at the first glance); keep in mind that we have a namespace called '​SF_'​ and our application is called '​storefront'​.
 +
 +<code php | application/​Bootstrap.php >
 +class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
 +{
 +    ...
 +    protected function _initConfig()
 +    {
 +    Zend_Registry::​set('​config',​ new Zend_Config($this->​getOptions()));​
 +    }
 +
 +    protected function _initAutoload()
 +    {
 +        $autoloader = new Zend_Application_Module_Autoloader(array(
 +            '​namespace'​ => '​SF_',​
 +            '​basePath' ​ => dirname(__FILE__),​
 +        ));
 +        return $autoloader; ​       ​
 +    }
 +
 +   ​protected function _initDoctrine() {
 +        // include and register Doctrine'​s class loader
 +        require_once('​Doctrine/​Common/​ClassLoader.php'​);​
 +        $classLoader = new \Doctrine\Common\ClassLoader(
 +            '​Doctrine',​
 +                    APPLICATION_PATH . '/​../​library/'​
 +        );
 +        $classLoader->​register();​
 +       
 +        // create the Doctrine configuration
 +        $config = new \Doctrine\ORM\Configuration();​
 +       
 +        // setting the cache (APC, Memcache, XCache, ArrayCache)
 +        if ($this->​getEnvironment() == '​production'​) {
 +            $cache = new \Doctrine\Common\Cache\ApcCache;​
 +        } else {
 +            $cache = new \Doctrine\Common\Cache\ArrayCache;​
 +        }        ​
 +        $config->​setMetadataCacheImpl($cache);​
 +        $config->​setQueryCacheImpl($cache);​
 +       
 +        // choosing the driver for our database schema
 +        // we'll use annotations (but you can use also XML or YAML style)
 +        $driver = $config->​newDefaultAnnotationDriver(
 +                APPLICATION_PATH . '/​models'​
 +        );
 +        $config->​setMetadataDriverImpl($driver);​
 +       
 +        // set the proxy dir and set some options
 +        $config->​setProxyDir(APPLICATION_PATH . '/​models/​Proxies'​);​
 +        $config->​setAutoGenerateProxyClasses(true);​
 +        $config->​setProxyNamespace('​Storefront\Proxies'​);​
 +
 +        // create the entity manager based on application.ini
 +        $database_config = Zend_Registry::​get('​config'​)->​doctrine;​
 +        $conn = array(
 +            '​driver' ​   => $database_config->​conn->​driv,​
 +            '​user' ​     => $database_config->​conn->​user,​
 +            '​password' ​ => $database_config->​conn->​pass,​
 +            '​dbname' ​   => $database_config->​conn->​dbname,​
 +            '​host' ​     => $database_config->​conn->​host,​
 +            '​charset'​ => $database_config->​conn->​charset,​
 +        );
 +        $entityManager = \Doctrine\ORM\EntityManager::​create($conn,​ $config);
 +       
 +        // push the entity manager into our registry for later use
 +        Zend_Registry::​set('​em',​ $entityManager);​
 +            ​
 +        //​Zend_Debug::​dump($entityManager,​ '​Entity Manager',​ true); // optional
 +        return $entityManager; ​   ​
 +    }
 +}
 +</​code>​
 +
 +
 +=== Adapt the CLI Doctrine Tool === 
 +
 +Doctrine comes with a tool called doctrine.php (or .bat for windows versions) but we need to process it a little bit to match our project.\\ ​
 +Create then a file called //​doctrine.php//​ under the //library// folder (where you actually put the Doctrine library in the first place).
 +
 +<code php | doctrine.php>​
 +define('​APPLICATION_ENV',​ '​development'​);​
 +define('​APPLICATION_PATH',​ realpath(dirname(__FILE__) . '/​../​application'​));​
 +
 +set_include_path(implode(PATH_SEPARATOR,​ array(
 +    realpath(APPLICATION_PATH . '/​../​library'​),​
 +    get_include_path(),​
 +)));
 +
 +
 +// doctrine setup
 +require_once '​Doctrine/​Common/​ClassLoader.php';​
 +
 +$classLoader = new \Doctrine\Common\ClassLoader('​Doctrine',​ APPLICATION_PATH . '/​../​library'​);​
 +$classLoader->​register();​
 +
 +$classLoader = new \Doctrine\Common\ClassLoader('​Symfony',​ APPLICATION_PATH . '/​../​library/​Doctrine'​);​
 +$classLoader->​register();​
 +
 +$classLoader = new \Doctrine\Common\ClassLoader('​Entities',​ APPLICATION_PATH . '/​models'​);​
 +$classLoader->​register();​
 +
 +
 +
 +// Zend Components
 +require_once '​Zend/​Application.php';​
 + 
 +// Create application
 +$application = new Zend_Application(
 +    APPLICATION_ENV,​
 +    APPLICATION_PATH . '/​configs/​application.ini'​
 +);
 +
 +// getting '​em'​
 +$application->​getBootstrap()->​bootstrap('​config'​);​
 +$application->​getBootstrap()->​bootstrap('​doctrine'​);​
 +$em = $application->​getBootstrap()->​getResource('​doctrine'​);​
 +
 +// generate the Doctrine HelperSet
 +$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);​
 +</​code>​
 +
 +
 +=== Create our first Entity ===
 +
 +Under models directory, create a file called Product.php with the following content:
 +
 +<code php | Product.php>​
 +<?php
 +/**
 + * @Entity
 + * @Table(name="​products"​)
 + */
 +class SF_Model_Product ​
 +{
 +    /**
 +     * @Id @Column(type="​integer"​)
 +     * @GeneratedValue(strategy="​AUTO"​) ​
 +     */
 +    private $product_id;​
 +    ​
 +    /**
 +     * @Column(type="​string",​ length=50, unique=TRUE)
 +     */
 +    private $product_name;​
 +    ​
 +    /**
 +     * @Column(type="​date"​)
 +     */
 +    private $product_date;​
 +    ​
 +
 +    public function setName($name) {
 +       ​$this->​product_name = $name;
 +       ​return true;
 +    }
 +     
 +    public function setDate($date = ''​) {
 +         ​$this->​product_date = new DateTime($date);​
 +    }
 +}
 +</​code>​
 +
 +
 +=== Use the tool, Luke! ===
 +
 +<code bash|Checking the new toys>
 +$ php doctrine.php orm:​schema-tool:​create
 +...
 +Creating database schema...
 +Database schema created successfully!
 +</​code>​
 +
 +At this point, you should have a table "​products"​ in your db environment.
 +
 +
 +=== Insert some data ===
 +
 +Practically,​ somewhere in the code you'll have this:
 +
 +<code php | Usage example>
 +$product = new SF_Model_Product();​
 +$product->​setName("​New product with utf8 chars ŞŞŢŢÎÎ"​);​
 +$product->​setDate();​ // empty means today, check setDate from SF_Model_Product
 +
 +$em = Zend_Registry::​get('​em'​);​
 +$em->​persist($product);​
 +$em->​flush();​ // this will do the magic
 +</​code>​
 +
  
php/zend_1_doctrine_2.txt · Last modified: 2013/03/16 17:40 (external edit)