• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Development / How To Use Doctrine With CodeIgniter

How To Use Doctrine With CodeIgniter

June 15, 2011 by Rana Ahsan 28 Comments

doctrine codeigniter

I have already discussed few articles on codeigniter along with codeigniter basics. Codeigniter has good library/mechanism for manipulating database activity. Besides, it introduced active record class, which minimizes the complexity of writing SQL query. However, database support in codeigniter isn’t still good enough for many. Specially, many developers prefer to work with ORM for database layer and CodeIgniter doesn’t provide such built-in support. However, doctrine is a very powerful ORM in php and it is possible to easily integrate and use doctrine with CodeIgniter as ORM.

There is an official help page to do so, On official documentation page. However, you might get some issues and can’t understand why those are appearing clearly, specially as a beginner, same happened to me. That’s why, i think a more detailed explanation may help you in the way. So, In this tutorial, i will show how can we integrate this doctrine with CodeIgniter. This will follow will show some basic usage with PHO code examples. I am assuming, you have some basic knowledge in CodeIgniter. Basic understanding about what doctrine is and how it works, are also needed. You can follow an official getting started with doctrine tutorial.

Download And Install:

First, please download the latest doctrine orm. Now extract the contents and copy the extracted ‘doctrine’ directory to your CodeIgniter application’s ‘third_party’ directory.

Alternatively, if you are a composer fan, and already using composer with codeigniter, you can install doctrine orm via adding dependency on composer.json as below:

"require": {
            "doctrine/orm": "dev-master"
}

Now, in ‘application/libraries’ directory, lets create a new class file named ‘doctrine.php’ that will include all initialization of our doctrine library for our CodeIgniter application. The class is as follows:

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\DBAL\Logging\EchoSQLLogger,
    Doctrine\ORM\Mapping\Driver\DatabaseDriver,
    Doctrine\ORM\Tools\DisconnectedClassMetadataFactory,
    Doctrine\ORM\Tools\EntityGenerator;

/**
 * CodeIgniter Smarty Class
 *
 * initializes basic doctrine settings and act as doctrine object
 *
 * @final	Doctrine 
 * @category	Libraries
 * @author	Md. Ali Ahsan Rana
 * @link	http://codesamplez.com/
 */
class Doctrine {

  /**
   * @var EntityManager $em 
   */
    public $em = null;

  /**
   * constructor
   */
  public function __construct()
  {
    // load database configuration from CodeIgniter
    require APPPATH.'config/database.php';
    
    // Set up class loading. You could use different autoloaders, provided by your favorite framework,
    // if you want to.
    require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';

    $doctrineClassLoader = new ClassLoader('Doctrine',  APPPATH.'third_party');
    $doctrineClassLoader->register();
    $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
    $entitiesClassLoader->register();
    $proxiesClassLoader = new ClassLoader('proxies', APPPATH.'models');
    $proxiesClassLoader->register();

    // 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);

    // Proxy configuration
    $config->setProxyDir(APPPATH.'models/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);   
  
    
  } 
}

NOTE: This above code requires php 5.3.x version installed on your server.

The above class is almost same as the one described on official reference, just optimized to be usable on codeigniter 2.x versions. Also, you will need to create two directories inside the ‘application/models’ directory, named ‘proxies’ and ‘entities’. On the above code, first we loaded codeigniter’s database configuration and doctrine class loaded to load necessary classes. Then it loads some necessary items. To know more in details about the configuration options and know how they works, please refer to official documentation of doctrine configuration options reference.

Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com

Create Entity From Database Automatically:

Although, its possible to generate entity classes from YAML file/ database through some command line argument, its much easier to have them generated right from the PHP script without any kind of extra effort. Following is an extra function that will help you to generate the entity classes. Just put it within the previously given class and call it at the last line on class constructor.

/**
   * generate entity objects automatically from mysql db tables
   * @return none
   */
  function generate_classes(){     
      
    $this->em->getConfiguration()
             ->setMetadataDriverImpl(
                new DatabaseDriver(
                        $this->em->getConnection()->getSchemaManager()
                )
    );

    $cmf = new DisconnectedClassMetadataFactory();
    $cmf->setEntityManager($this->em);
    $metadata = $cmf->getAllMetadata();     
    $generator = new EntityGenerator();
    
    $generator->setUpdateEntityIfExists(true);
    $generator->setGenerateStubMethods(true);
    $generator->setGenerateAnnotations(true);
    $generator->generate($metadata, APPPATH."models/Entities");
    
  }

Remember, activate calling to this method only once. and then after the entity classes are generated, keep it commented, otherwise you may get errors. If sometime, database structure changes/tables added/removed, just clear the ‘entities’ directory and reactivate the method(one time only, as well).

Basic Usage Of Doctrine With CodeIgniter:

Lets do a contact form example to understand how to use doctrine ORM within CodeIgniter. Lets create a table on our MySQL database named ‘pd_contact’ as like follows:
mysql contact table schema example

This will generate and entity class as like follows:

<?php
/**
 * PdContact
 *
 * @Table(name="pd_contact")
 * @Entity
 */
class PdContact
{
    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $name
     *
     * @Column(name="name", type="string", length=50, nullable=false)
     */
    private $name;

    /**
     * @var string $email
     *
     * @Column(name="email", type="string", length=50, nullable=false)
     */
    private $email;

    /**
     * @var string $subject
     *
     * @Column(name="subject", type="string", length=100, nullable=false)
     */
    private $subject;

    /**
     * @var text $message
     *
     * @Column(name="message", type="text", nullable=false)
     */
    private $message;


    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get email
     *
     * @return string $email
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set subject
     *
     * @param string $subject
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

    /**
     * Get subject
     *
     * @return string $subject
     */
    public function getSubject()
    {
        return $this->subject;
    }

    /**
     * Set message
     *
     * @param text $message
     */
    public function setMessage($message)
    {
        $this->message = $message;
    }

    /**
     * Get message
     *
     * @return text $message
     */
    public function getMessage()
    {
        return $this->message;
    }
}

Now, lets create a model that will contain the database layer logic to insert submitted data from a contact form. Here it is:

require_once(APPPATH."models/Entities/PdContact.php");
use \PdContact;
/**
 * manipulates data and contains data access logics for Enity 'User'
 *
 * @final Homemodel
 * @category models 
 * @author Md. Ali Ahsan Rana
 * @link http://codesamplez.com
 */
class Homemodel extends CI_Model {
    
    /**     
     * @var \Doctrine\ORM\EntityManager $em 
     */
    var $em;
    
    public function __construct() {
        parent::__construct();
        $this->em = $this->doctrine->em;
    }
    
    /**
     * Add contact messages to database
     * @param array $contact_form
     * @return bool 
     */
    function add_message()
    {    
        /**
         * @var PdContact $contact
         */
        $contact = new PdContact();
        $contact->setName($this->input->post("name");
        $contact->setEmail($this->input->post("email");
        $contact->setSubject($this->input->post("subject");
        $contact->setMessage($this->input->post("message");
        
        try {
            //save to database
            $this->em->persist($contact);
            $this->em->flush();
        }
        catch(Exception $err){
            
            die($err->getMessage());
        }
        return true;        
    }
}

the above example will insert a new row to ‘pd_contact’ table with the posted data. Query execution will be in action when the “$this->em->flush();” is called. If there are more database operations, then those will be executed in this stage as a batch execution, which results faster performance then traditional query execution.

Using Doctrine Command Line Tool With Codeigniter:

If you are a CLI tool fan, you will might be more happier to know that, doctrine has support for command line! to add its support, we will have to create a file named ‘cli-config.php’ in current directory and add the following code in there:

$system_path = 'system';
$application_folder = 'application';
define('BASEPATH', str_replace("\\", "/", $system_path));
define('APPPATH', $application_folder.'/');
        
include __DIR__."/vendor/autoload.php";
include __DIR__."/application/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)
));

return $helperSet;

Now we should be fine to use the tool. If all settings are OK, run ‘{path}/{to}/{cli}/doctrine’ and you should see some out like the below screenshot:

doctrine cli dedault output

Other examples of commands can be as like below:

$./vendor/bin/doctrine orm:validate-schema
$./vendor/bin/doctrine orm:generate:proxies
....

Note that, here I assumed you are using composer installed doctrine. Otherwise, use the proper cli tool path instead of ‘./vendor/bin/doctrine’.

Final Words:

Hope this tutorial on using doctrine with CodeIgniter will be very helpful to have a basic start for you. Ask if you have any questions or facing any issues. Keep in touch for more useful tutorials. Happy coding 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Development Tagged With: codeigniter, doctrine, php

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Mehmet Aydın Bahadır says

    September 8, 2011 at 4:35 am

    Hi, thanks for this great tutorial. But i have a problem. When i close generate_classes() function, taking an error: Uncaught exception ‘Doctrine\ORM\Mapping\MappingException’ with message ‘Class Actions is not a valid entity or mapped super class.’

    But after open this:
    $this->em->getConfiguration()
    ->setMetadataDriverImpl(
    new DatabaseDriver(
    $this->em->getConnection()->getSchemaManager()
    )
    );

    there’s no error but website is very slow. What can i do? I do everything as you say.

    Reply
  2. dionysis says

    October 28, 2011 at 9:36 am

    function generate_classes()
    {
    $this->em->getConfiguration()
    ->setMetadataDriverImpl(
    new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
    $this->em->getConnection()->getSchemaManager()
    )
    );

    $cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
    $cmf->setEntityManager($this->em);
    $metadata = $cmf->getAllMetadata();
    $generator = new \Doctrine\ORM\Tools\EntityGenerator();

    $generator->setUpdateEntityIfExists(true);
    $generator->setGenerateStubMethods(true);
    $generator->setGenerateAnnotations(true);
    $generator->generate($metadata, APPPATH.”models/Entities”);
    }

    Reply
  3. Martin says

    November 3, 2011 at 5:37 pm

    Auto generating entity classes code is not working..

    Reply
    • Rana says

      November 21, 2011 at 12:15 pm

      What issue you are having please?

      Reply
  4. nicx says

    January 15, 2012 at 11:18 pm

    hi im totally a newbie with this doctrine, and i willing to learn. i cant figure out how to integrate doctrine and CI, need help. thanks in advance. when i will call the generate classes? DQL is like HQL but netbeans IDE generates entities easily. need help with this doctrine stuff. thanks

    Reply
    • Rana says

      July 16, 2012 at 3:48 am

      you will need to call the generate class function only once, when you run the application on browser first time. Please let me know specifically in which other area you need help. Thanks.

      Reply
  5. mehmet soylu says

    May 18, 2012 at 5:54 pm

    Fatal error: Uncaught exception ‘Doctrine\DBAL\DBALException’ with message ‘Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.’ in D:\Projects\ci\application\third_party\doctrine-orm\Doctrine\DBAL\Platforms\AbstractPlatform.php:210

    Reply
    • Rana says

      May 19, 2012 at 8:38 am

      Hi, when did you get this error please? did you created the databases and tables? My tutorial assumes so. by doctrine, its also possible to generate tables structure in db from running the application, but I didn’t cover that part in this tutorial.

      Reply
    • alok kumar saxena says

      February 16, 2016 at 1:13 am

      Hi hehmet soylu,
      You have to add just two lines of code in generate_class function.

      $platform = $this->em->getConnection()->getDatabasePlatform();
      $platform->registerDoctrineTypeMapping(‘enum’, ‘string’);

      Thanks
      Alok

      Reply
  6. sivaram says

    October 9, 2012 at 4:22 am

    Class “pdcontact” sub class of “” is not a valid entity or mapped super class.

    i am facing this error what is the actual problem

    Reply
    • Rana says

      November 20, 2012 at 3:34 am

      You have probably forgot to create the table in database I guess. If not, can you please share the details error screenshot please?

      Reply
  7. Rods says

    December 6, 2012 at 6:37 pm

    Doesn’t work for me! I used git clone…but I’m receiving this error message:
    Message: require_once(application/third_party/Doctrine/Common/ClassLoader.php) [function.require-once]: failed to open stream: No such file or directory

    Filename: libraries/Doctrine.php

    Reply
    • Rana says

      December 6, 2012 at 11:15 pm

      Seems like, your file path is incorrect. can you please verify whether the file ‘ClassLoader.php’ is located at this path “application/third_party/Doctrine/Common/ClassLoader.php”?

      Reply
      • Rods says

        December 7, 2012 at 9:07 am

        Thank’s very much. It was a wrong file path!

        Reply
  8. Jitesh says

    January 17, 2013 at 2:29 am

    Hi,

    That’s nice but can you provide the download link for the source code.
    It would help the newbee(beginners a lot).

    Thanks
    Raj

    Reply
    • Md. Ali Ahsan Rana says

      January 17, 2013 at 2:43 am

      Hi Raj, I actually don’t have the code right now that I used on this tutorial. However, as a reference, you can use my ‘codeigniterplus’ project, https://github.com/ranacseruet/codeigniterplus, it has ready integration of doctrine in codeigniter and there I implemented some basic doctrine functionality. Hope it will help you. Let me know if you need anything else. Keep in touch. Thanks.

      Reply
  9. Latha says

    November 26, 2013 at 1:54 am

    hi., can anybody help me., how to authenticate username and password stored in db..??? i am newbie to code ignitor and doctrine. help me plz..

    Reply
  10. pusp raj joshi says

    July 31, 2014 at 12:58 pm

    How to fetch data in view?
    (
    [users] => Array
    (
    [0] => Users Object
    (
    [id:Users:private] => 1
    [ipAddress:Users:private] => 127.0.0.1
    [username:Users:private] => administrator
    [password:Users:private] => $2a$07$SeBknntpZror9uyftVopmu61qg0ms8Qv1yV6FG.kQOSM.9QhmTo36
    [salt:Users:private] =>
    [email:Users:private] => admin@admin.com
    [activationCode:Users:private] =>
    [forgottenPasswordCode:Users:private] =>
    [forgottenPasswordTime:Users:private] =>
    [rememberCode:Users:private] =>
    [createdOn:Users:private] => 1268889823
    [lastLogin:Users:private] => 1406821784
    [active:Users:private] => 1
    [firstName:Users:private] => Admin
    [lastName:Users:private] => istrator
    [company:Users:private] => ADMIN
    [phone:Users:private] => 0
    )

    )

    )

    Reply
    • Md Ali Ahsan Rana says

      August 15, 2014 at 1:32 am

      You can simply use “$users[0]->getEmail()” (iterate for multiple entries) and similar syntax to show if on view. Hope this helps!

      Reply
  11. Musa Haidari says

    September 20, 2014 at 10:33 pm

    Thank you to mention this part. Only I wanted to mention that I had the same problem but got confused how to solve this. What I did to solve it was to move the following piece code to the constructor of the Doctrine class, before the $this->generate_classes (); so it looks like:

    class Doctrine {

    /**
    * @var EntityManager $em
    */

    public $em = null;

    /**
    * constructor
    */

    public function __construct()
    {

    //Other codes

    //move this piece of code here
    $this->em->getConfiguration()
    ->setMetadataDriverImpl(
    new DatabaseDriver(
    $this->em->getConnection()->getSchemaManager()
    )
    );

    $this->generate_classes();
    }
    } // end of Doctrine class

    Reply
  12. tahseen says

    February 18, 2015 at 12:30 am

    i have followed your tutorial step by step working fine. when i comment out the function call in constructor and which create the entity classes .. and call the entity class in model it shows this error.

    Class “Options” is not a valid entity or mapped super class.

    any idea please…

    Reply
  13. omur says

    July 28, 2015 at 4:11 am

    Please help i cant solve this problem.

    Fatal error: Uncaught exception ‘Doctrine\ORM\Mapping\MappingException’ with message ‘Class “Entity\User” is not a valid entity or mapped super class.’ in /home/u172335430/public_html/CI_Doctrine/application/libraries/Doctrine/ORM/Mapping/MappingException.php:336 Stack trace: #0 /home/u172335430/public_html/CI_Doctrine/application/libraries/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php(89): Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass(‘Entity\\User’) #1 /home/u172335430/public_html/CI_Doctrine/application/libraries/Doctrine/ORM/Mapping/ClassMetadataFactory.php(117): Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass(‘Entity\\User’, Object(Doctrine\ORM\Mapping\ClassMetadata)) #2 /home/u172335430/public_html/CI_Doctrine/application/libraries/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(318): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array) #3 /home/u172335430/public_html/CI_D in /home/u172335430/public_html/CI_Doctrine/application/libraries/Doctrine/ORM/Mapping/MappingException.php on line 336

    Reply
  14. janis says

    December 1, 2016 at 5:30 am

    i got this message when test this tutorial Class “PdContact” is not a valid entity or mapped super class.
    so i need help to understantd

    Reply

Trackbacks

  1. Doctrine CRUD Operation Basics With Code Examples | codesamplez.com says:
    July 18, 2012 at 11:49 pm

    […] take help of several online tutorials as well. On an earlier post, I have already discussed about basic usage and integration of Doctrine ORM with Codeigniter framework. Today, in this tutorial, I am going to give some code examples of how we can perform the basic […]

    Reply
  2. Codeigniter Bundle With Popular Libraries And Best Practices | codesamplez.com says:
    December 20, 2012 at 2:20 am

    […] The Power of ORM: If you are not familiar with doctrine ORM, you should start by using doctrine with Codeigniter tutorial. As there are some functionality implemented here, you will feel how easy/efficient this […]

    Reply
  3. Custom Codeigniter Doctrine Model With Basic Functionality | codesamplez.com says:
    December 20, 2012 at 2:39 am

    […] use doctrine before or new with Doctrine in Codeigniter, feel free to read my another article on using Doctrine ORM with Codeigniter application […]

    Reply
  4. Codeigniter Bundle With Popular Libraries And Best PracticesCodeSamplez.com says:
    December 21, 2012 at 8:04 am

    […] The Power of ORM: If you are not familiar with doctrine ORM, you should start by using doctrine with Codeigniter tutorial. As there are some functionality implemented here, you will feel how easy/efficient this […]

    Reply
  5. Tips On Codeigniter Best Practices For DevelopmentCodeSamplez.com says:
    December 24, 2012 at 3:27 am

    […] you may want to use third party library, doctrine is my choice. You can read my earlier article on integrating doctrine with Codeigniter. Also, they maintain a very good documentation to start with, if you are interested to start with […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#
  • Getting Started With UDP Programming in Java
    Getting Started With UDP Programming in Java
  • Utilizing Config File In C#.NET Application
    Utilizing Config File In C#.NET Application

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023