Credo is a special library that easily integrates Doctrine ORM into the Codeigniter framework. It is also based on Doctrine's official integration for Codeigniter.
Install Credo
through Composer:
$ composer require rougin/credo
// application/models/User.php
/**
* @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
$this->load->model('user');
$this->load->database();
$credo = new Rougin\Credo\Credo($this->db);
$repository = $credo->get_repository('User');
$user = $repository->findBy(array());
Extend Rougin\Credo\Loader
to MY_Loader
:
// application/core/MY_Loader.php
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
use Rougin\Credo\Repository;
class User_repository extends Repository
{
public function find_by_something()
{
// ...
}
}
Then load the repository using $this->load->repository($repositoryName)
.
// application/controllers/Welcome.php
$this->load->model('user');
// Loads the customized repository
$this->load->repository('user');
$this->load->database();
$credo = new Rougin\Credo\Credo($this->db);
$repository = $credo->get_repository('User');
$users = $repository->find_by_something();
For more information about repositories in Doctrine, please check its documentation.
// application/models/User.php
/**
* @Entity
* @Table(name="user")
*/
class User extends \Rougin\Credo\Model
{
/**
* @Id @GeneratedValue
* @Column(name="id", type="integer", length=10, nullable=FALSE, unique=FALSE)
* @var integer
*/
protected $_id;
// ...
}
// application/controllers/Welcome.php
$this->load->model('user', '', TRUE);
$this->user->credo(new Rougin\Credo\Credo($this->db));
$users = $this->user->get();
The new release for v0.5.0
will be having a backward compatibility break (BC break). So some functionalities on the earlier versions might not be working after updating. This was done to increase the maintainability of the project while also adhering to the functionalities for both Codeigniter and Doctrine ORM. To check the documentation for the last release (v0.4.0
), kindly click here.
Before
class User extends \Rougin\Credo\CodeigniterModel {}
After
class User extends \Rougin\Credo\Model {}
Before
class User extends \Rougin\Credo\EntityRepository {}
After
class User extends \Rougin\Credo\Repository {}
Before
$this->load->model('user');
After
$this->load->model('user');
$credo = new Rougin\Credo\Credo($this->db);
$this->user->credo($credo);
All Credo functionalities are now moved to CredoTrait
which is can be added on existing models.
Before
// PaginateTrait::paginate($perPage, $config = array())
list($result, $links) = $this->user->paginate(5, $config);
After
$total = $this->db->count_all_results('users');
// PaginateTrait::paginate($perPage, $total, $config = array())
list($offset, $links) = $this->user->paginate(5, $total, $config);
The total count must be passed in the second parameter.
Before
$total = $this->user->countAll();
After
$total = $this->db->count_all_results('users');
This is being used only in PaginateTrait::paginate
.
Before
ValidateTrait::validationErrors()
After
ValidateTrait::errors()
Before
// application/models/User.php
protected $validation_rules = array();
After
// application/models/User.php
protected $rules = array();
Before
$users = $this->user->all();
After
$users = $this->user->get();
Please see CHANGELOG for more information what has changed recently.
$ composer test
The MIT License (MIT). Please see LICENSE for more information.