PHP code example of rougin / credo

1. Go to this page and download the library: Download rougin/credo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rougin / credo example snippets

 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';
 php
// 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;

    // ...
}
 php
// 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());
 php
// ciacme/application/core/MY_Loader.php

use Rougin\Credo\Loader;

class MY_Loader extends Loader
{
}
 php
// ciacme/application/repositories/User_repository.php

use Rougin\Credo\Repository;

class User_repository extends Repository
{
    public function find_by_something()
    {
        // ...
    }
}
 php
// 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();
 php
// ciacme/application/controllers/Welcome.php

use Rougin\Credo\Credo;

// ...

// $this->db must not be ;
// -----------------------------------------

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

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

// ...

/** @var \User[] */
$users = $this->user->get();
 php
// 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;

    // ...
}
 php
// 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;

    // ...
}
 php
// 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();
 php
// 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);
 php
// ciacme/application/views/users/index.php

 echo $links; 
 php
// 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<string, string>[]
     */
    protected $rules = array(
        array('field' => 'name', 'label' => 'Name', 'rules' => '
 php
// ciacme/application/controllers/Welcome.php

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

$valid = $this->user->validate($input);
 php
// ciacme/application/views/users/create.php

<?= form_open('users/create')