Install Codeigniter 3 with Doctrine and Composer

8

How best to install via

asked by anonymous 04.05.2015 / 23:35

1 answer

5

TL; DR

I uploaded the GitHub sample project by integrating CodeIgniter 3 with Doctrine, with an example.

The error is in the way that you use autoloading .

In the doctrine.php file the correct one on line 3 would be:

$loader = require APPPATH . 'vendor/autoload.php';

But to include the autoload inside the Doctrine.php is unnecessary in Codeigniter 3. It has a configuration that already injects the autoload of the Composer into our application.

Set the file config.php

$config['composer_autoload'] = TRUE;

Or if you prefer to use the vendor folder in the project root (my case), enter the path to autoload.php .

$config['composer_autoload'] = 'vendor/autoload.php';

Using the second option we can take from our composer.json to "vendor-dir" : "application/libraries" .

My libraries\Doctrine.php file is a bit different too. I used a based SOen response file.

<?php

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager;

class Doctrine
{
    public $em;

    public function __construct()
    {
        // Load the database configuration from CodeIgniter
        require APPPATH . 'config/database.php';

        $connection_options = array(
            'driver'        => 'pdo_mysql',
            'user'          => $db['default']['username'],
            'password'      => $db['default']['password'],
            'host'          => $db['default']['hostname'],
            'dbname'        => $db['default']['database'],
            'charset'       => $db['default']['char_set'],
            'driverOptions' => array(
                'charset'   => $db['default']['char_set'],
            ),
        );

        // With this configuration, your model files need to be in application/models/Entity
        // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
        $models_namespace = 'Entity';
        $models_path = APPPATH . 'models';
        $proxies_dir = APPPATH . 'models/Proxies';
        $metadata_paths = array(APPPATH . 'models');

        // Set $dev_mode to TRUE to disable caching while you develop
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);

        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();
    }
}

To use Doctrine , create a Entity folder within models and insert therein your Entities within the Entity namespace

<?php namespace Entity;

/**
 * @Entity @Table(name="products")
 **/
class Product
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

After adding the Doctrine.php to the autoload of Codeigniter ( $autoload['libraries'] = array('doctrine'); ) I can use Entity in my controller . >

public function index()
{
    $product = new Entity\Product();

    $product->setName('Teste');

    $this->doctrine->em->persist($product);
    $this->doctrine->em->flush();       
}
    
13.05.2015 / 15:45