Credo is a simple PHP library that uses Doctrine ORM into the Codeigniter framework for managing database logic.
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());
Rougin\Credo\Repository
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.
Rougin\Credo\Model
// 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();
v0.5.0
releaseThe 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.
CodeigniterModel
class to Model
classBefore
class User extends \Rougin\Credo\CodeigniterModel {}
After
class User extends \Rougin\Credo\Model {}
EntityRepository
class to Repository
classBefore
class User extends \Rougin\Credo\EntityRepository {}
After
class User extends \Rougin\Credo\Repository {}
Credo
instance manually to Model
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.
PaginateTrait::paginate
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.
Model::countAll
Before
$total = $this->user->countAll();
After
$total = $this->db->count_all_results('users');
This is being used only in PaginateTrait::paginate
.
ValidateTrait::validationErrors
to ValidateTrait::errors
Before
ValidateTrait::validationErrors()
After
ValidateTrait::errors()
ValidateTrait::validation_rules
to ValidateTrait::rules
Before
// application/models/User.php
protected $validation_rules = array();
After
// application/models/User.php
protected $rules = array();
Model::all
to Model::get
Before
$users = $this->user->all();
After
$users = $this->user->get();