Welcome Guest, Not a member yet? Register   Sign In
Credo - Integrates Doctrine to CodeIgniter with ease
#1

(This post was last modified: 03-21-2016, 06:14 PM by rougin.)

Hello! I've created a library named Credo, a synonym of the word Doctrine (www.doctrine-project.org/projects/orm.html) Big Grin, that integrates the Doctrine ORM easily in CodeIgniter. http://github.com/rougin/credo

Installation

You can install it via Composer:

Code:
$ composer require rougin/credo

NOTE: composer_autoload must be enabled in the application/config/config.php.

Basic Usage

application/models/User.php

PHP Code:
/**
 * @Entity
 * @Table(name="user")
 */
class User extends CI_Model {

 
   /**
     * @Id @GeneratedValue
     * @Column(name="id", type="integer", length=10, nullable=FALSE, unique=FALSE)
     * @var integer
     */
 
   protected $_id;

 
   // ...



application/controllers/Welcome.php

PHP Code:
$this->load->model('user');
$this->load->database();

$credo = new Rougin\Credo\Credo($this->db);
$repository $this->credo->get_repository('User');
$user $repository->find(4); 

Using Doctrine's EntityRepository

Extend Rougin\Credo\Loader to MY_Loader.

application/core/MY_Loader.php

PHP Code:
class MY_Loader extends Rougin\Credo\Loader {} 

Kindly also use the suffix _repository for creating repositories. (e.g. User_repository)

application/repositories/User_repository.php

PHP Code:
class User_repository extends Rougin\Credo\EntityRepository {
 
   // Other stuff...


NOTE: Extending your repository to Rougin\Credo\EntityRepository enables you to call its methods in underscore case. (e.g. find_all, find_by)

You can now load a repository by using this code: $this->load->repository($repositoryName).

application/controllers/Welcome.php

PHP Code:
$this->load->model('user');
$this->load->repository('user');
$this->load->database();

$credo = new Rougin\Credo\Credo($this->db);
$repository $this->credo->get_repository('User');
$user $repository->find(4); 

For more information about repositories in Doctrine, you can find them here: http://doctrine-orm.readthedocs.org/proj...positories
Reply
#2

Hi,

Don't know much about Doctrine, and congrats on the integration.

But would it not be easier to just do this:
Code:
$user = $this->db->where('id', 4)->get('users');

Or is there something about Doctrine that I have missed?

Paul.
Reply
#3

(This post was last modified: 03-20-2016, 09:24 PM by rougin.)

Hello @PaulD,

I understand that the code you mentioned is much easier but for me, I just followed the specifications provided by the Doctrine ORM. What I did was I made some methods that can be called in underscore case since it is in the Style Guide of CodeIgniter such as getRepository to get_repository. I did also based my implementation from the Getting Started page of Doctrine ORM (http://docs.doctrine-project.org/project...arted.html).
Reply
#4

(This post was last modified: 03-21-2016, 02:21 AM by Avenirer.)

So, you went through all that just to follow the style guide?... Why would you? Smile If someone were to use Doctrine, why not use it the way that the user manual shows him/her? Why add complexity when there is no need?
Reply
#5

Hello @Avenirer,

Not exactly. It's just only an option for other users if they want to follow the CodeIgniter's style guide.
Reply
#6

Hello! Credo is now updated. It fixes some issues in declaring the Credo class. Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB