PHPackages                             rougin/credo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. rougin/credo

ActiveLibrary[Database &amp; ORM](/categories/database)

rougin/credo
============

Use Doctrine ORM for Codeigniter 3.

v0.6.1(1mo ago)151.2k3MITPHPPHP &gt;=5.4.0CI failing

Since Mar 20Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/rougin/credo)[ Packagist](https://packagist.org/packages/rougin/credo)[ Docs](https://roug.in/credo/)[ RSS](/packages/rougin-credo/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (10)Versions (11)Used By (0)

Credo
=====

[](#credo)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b9335c14a0a61e94a0cf27b84745dd114f259379d2c2293194d60cc7036c0562/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f7567696e2f637265646f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/credo)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rougin/credo/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/e09087d1737f45e8d9e100131c9ed08251adc4bb9aec711661d67d862f8696c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f7567696e2f637265646f2f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/rougin/credo/actions)[![Coverage Status](https://camo.githubusercontent.com/708d9a7452493176ef96f1f7cba355a4e8c76c02d61fdb6b617718c02b0e81b5/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f7567696e2f637265646f3f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/rougin/credo)[![Total Downloads](https://camo.githubusercontent.com/0c7abaf2fbe890075936a1755d76434c21df3b29c09e304ee306859b5e36ea51/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f7567696e2f637265646f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/credo)

Credo is a wrapper package of [Doctrine ORM](http://www.doctrine-project.org/projects/orm.html) for [Codeigniter 3](https://codeigniter.com/userguide3/). Its implementation was created based on the official [integration for Codeigniter 3](https://web.archive.org/web/20180624142647/https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/integrating-with-codeigniter.html) to the `Doctrine ORM` package.

Installation
------------

[](#installation)

Install `Credo` via [Composer](https://getcomposer.org/):

```
$ composer require rougin/credo
```

Basic Usage
-----------

[](#basic-usage)

Create a sample database table first to be used in this example (e.g., `users`):

```
-- Import this script to a SQLite database

CREATE TABLE user (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    name TEXT NOT NULL,
    age INTEGER NOT NULL,
    gender TEXT NOT NULL
);

INSERT INTO user (name, age, gender) VALUES ('Rougin', 20, 'male');
INSERT INTO user (name, age, gender) VALUES ('Royce', 18, 'male');
INSERT INTO user (name, age, gender) VALUES ('Mei', 19, 'female');
```

Then configure the `composer_autoload` option in `config.php`:

```
// ciacme/application/config/config.php

/*
|--------------------------------------------------------------------------
| Composer auto-loading
|--------------------------------------------------------------------------
|
| Enabling this setting will tell CodeIgniter to look for a Composer
| package auto-loader script in application/vendor/autoload.php.
|
|   $config['composer_autoload'] = TRUE;
|
| Or if you have your vendor/ directory located somewhere else, you
| can opt to set a specific path as well:
|
|   $config['composer_autoload'] = '/path/to/vendor/autoload.php';
|
| For more information about Composer, please visit http://getcomposer.org/
|
| Note: This will NOT disable or override the CodeIgniter-specific
|   autoloading (application/config/autoload.php)
*/
$config['composer_autoload'] = __DIR__ . '/../../vendor/autoload.php';
```

Note

The value of `composer_autoload` should be the `vendor` directory (e.g., `ciacme/vendor/autoload.php`).

Next is to create an entity that conforms to the [documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html#an-example-model-bug-tracker) of `Doctrine ORM` (e.g., `User`):

```
// ciacme/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;

    // ...
}
```

Once the entity is created, it can now be used to perform operations using the `Credo::getRepository`:

```
// ciacme/application/controllers/Welcome.php

use Rougin\Credo\Credo;

$this->load->model('user');

$this->load->database();

$credo = new Credo($this->db);

// Snake-case versions of the EntityManager ---
// methods are also available in the class ----
/** @var \Doctrine\ORM\EntityRepository */
$repository = $credo->get_repository('User');
// --------------------------------------------

/** @var \User[] */
$user = $repository->findBy(array());
```

Using `Rougin\Credo\Repository`
-------------------------------

[](#using-rougincredorepository)

To enable this package on a `Codeigniter 3` project, create a `MY_Loader` class first in the `core` directory then extend the newly created class to `Rougin\Credo\Loader`:

```
// ciacme/application/core/MY_Loader.php

use Rougin\Credo\Loader;

class MY_Loader extends Loader
{
}
```

Next is create a custom entity repository with a `_repository` suffix in the class name (e.g., `User_repository`):

```
// ciacme/application/repositories/User_repository.php

use Rougin\Credo\Repository;

class User_repository extends Repository
{
    public function find_by_something()
    {
        // ...
    }
}
```

Once the custom repository is created (e.g., `User_repository`), add the `repositoryClass` property inside the `@Entity` annotation of the specified entity to attach the said custom repository:

```
// ciacme/application/models/User.php

/**
 * @Entity(repositoryClass="User_repository")
 *
 * @Table(name="user")
 */
class User extends CI_Model
{
    // ...
}
```

Then load the specified repository using `$this->load->repository`:

```
// ciacme/application/controllers/Welcome.php

use Rougin\Credo\Credo;

// Load the model and its repository ---
$this->load->model('user');

$this->load->repository('user');

$this->load->database();
// -------------------------------------

$credo = new Credo($this->db);

// The said repository can now be used ------
/** @var \User_repository */
$repository = $credo->get_repository('User');
// ------------------------------------------

$users = $repository->find_by_something();
```

Note

It is encouraged to check the [documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html#guide-assumptions) about `Doctrine ORM` first for more information about its design pattern and its various usage on existing projects.

Using `~3.0` version of `Doctrine ORM`
--------------------------------------

[](#using-30-version-of-doctrine-orm)

`Credo` should be able to support the latest version of `Doctrine ORM` (`~3.0`). To use the latest version, the code must be slightly updated:

```
// ciacme/application/controllers/Welcome.php

use Rougin\Credo\Credo;

// ...

// $this->db must not be included as it ----
// will create an EntityManager instance ---
// based from the given Database object ----
$credo = new Credo;
// -----------------------------------------

// Create an implementation of EntityManager ---
$manager = /** sample implementation */;
// ---------------------------------------------

// Then attach it to the Credo instance ---
$credo->setManager($manager);
// ----------------------------------------

// ...

/** @var \User[] */
$users = $this->user->get();
```

Using this approach allows to use the latest improvements provided by `Doctrine ORM` like using the native attributes introduced in PHP `v8.1`:

```
// ciacme/application/models/User.php

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'users')]
class User
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    private int|null $id = null;

    #[ORM\Column(type: 'string')]
    private string $name;

    // ...
}
```

Tip

Please see the [Getting Started](https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html) documentation of `Doctrine ORM` on how to initialize an `EntityManager` in the latest version.

Using `Rougin\Credo\Model`
--------------------------

[](#using-rougincredomodel)

The `Model` class enables the specified entity to perform CRUD operations without relying on a repository:

```
// ciacme/application/models/User.php

use Rougin\Credo\Model;

/**
 * @Entity
 * @Table(name="user")
 */
class User extends Model
{
    /**
     * @Id @GeneratedValue
     * @Column(name="id", type="integer", length=10, nullable=FALSE, unique=FALSE)
     * @var integer
     */
    protected $_id;

    // ...
}
```

```
// ciacme/application/controllers/Welcome.php

use Rougin\Credo\Credo;

$this->load->model('user');

// Credo is not needed as it will try to ---
// create an instance based on $this->db ---
// $credo = new Credo($this->db);

// $this->user->credo($credo);
// -----------------------------------------

/** @var \User[] */
$users = $this->user->get();
```

The `Model` class contains methods for performing CRUD operations which are based both on the `Query Builder` class of `Codeigniter 3` and the `EntityManager` of `Doctrine ORM`.

Warning

This may be used for getting started to use the models directly without a repository. However, this will be against the principle of `Unit of Work` pattern by `Doctrine ORM` (e.g., using the entity class instead of an array in updating its data). With this, using an entity repository is highly encouraged.

Using Traits
------------

[](#using-traits)

`Credo` provides traits that are based from the libraries of `Codeigniter 3` such as `Form Validation` and `Pagination Class`. They are used to easily attach the specified functionalities of `Codeigniter 3` to a model.

### `PaginateTrait`

[](#paginatetrait)

The `PaginateTrait` is used to easily create pagination links within the model:

```
// ciacme/application/models/User.php

use Rougin\Credo\Model;
use Rougin\Credo\Traits\PaginateTrait;

class User extends Model
{
    use PaginateTrait;

    // ...
}
```

```
// ciacme/application/controllers/Welcome.php

// Create a pagination links with 10 as the limit and
// 100 as the total number of items from the result.
$result = $this->user->paginate(10, 100);

$data = array('links' => $result[1]);

$offset = $result[0];

// The offset can now be used for filter results
// from the specified table (e.g., "users").
$items = $this->user->get(10, $offset);
```

The `$result[0]` returns the computed offset while `$result[1]` returns the generated pagination links:

```
// ciacme/application/views/users/index.php

```

To configure the pagination library, the `$pagee` property must be defined in the `Model`:

```
// ciacme/application/models/User.php

use Rougin\Credo\Model;
use Rougin\Credo\Traits\PaginateTrait;

class User extends Model
{
    use PaginateTrait;

    // ...

    /**
     * Additional configuration to Pagination Class.
     *
     * @link https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination
     *
     * @var array
     */
    protected $pagee = array(
        'page_query_string' => true,
        'use_page_numbers' => true,
        'query_string_segment' => 'p',
        'reuse_query_string' => true,
    );
}
```

Note

Please see the documentation of [Pagination Class](https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination) to get the list of its available configuration.

### `ValidateTrait`

[](#validatetrait)

This trait is used to simplify the specifying of validation rules to a model:

```
// ciacme/application/models/User.php

use Rougin\Credo\Model;
use Rougin\Credo\Traits\ValidateTrait;

class User extends Model
{
    use ValidateTrait;

    // ...
}
```

When used, the `$rules` property of the model must be defined with validation rules that conforms to the `Form Validation` specification:

```
// ciacme/application/models/User.php

use Rougin\Credo\Model;
use Rougin\Credo\Traits\ValidateTrait;

class User extends Model
{
    use ValidateTrait;

    // ...

    /**
     * List of validation rules.
     *
     * @link https://codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array
     *
     * @var array[]
     */
    protected $rules = array(
        array('field' => 'name', 'label' => 'Name', 'rules' => 'required'),
        array('field' => 'email', 'label' => 'Email', 'rules' => 'required'),
    );
}
```

Note

Kindly check [its documentation](https://codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array) for the available rules that can be used to the `$rules` property.

To do a form validation, the `validate` method must be called from the model:

```
// ciacme/application/controllers/Welcome.php

/** @var array */
$input = $this->input->post(null, true);

$valid = $this->user->validate($input);
```

If executed with a view, the validation errors can be automatically be returned to the view using the `form_error` helper:

```
// ciacme/application/views/users/create.php
