On an earlier tutorial, I did showed how we can easily perform the CRUD operations using doctrine ORM. However, as a lot of developers use doctrine ORM as part of their favorite Codeigntier framework, I came to feel that, it will be very useful if we can use some wrapper model for these base CRUD operations in Codeigniter, that will use doctrine implementation. This should facilitate developers a KickAss start with codeigniter-Doctrine application development as developers usually spend a lot of time in code duplication for these base select, update, insert, delete operations.
The Plugin Script:
As a result, I have written a extended core codeigniter class ‘MY_DModel'(‘D’ stands for doctrine), which utilizes doctrine ORM and do the functionality of select, insert, update, delete etc general operations. So, for this regular operations, you can use these ready functions instead of writing your own.
You can find this project on github on the following url:
https://github.com/ranacseruet/Doctrine-CRUD-CodeIgniter
I have only implemented the most regular operations. It is possible that, I might have missed some, in that case, please comment with your suggestion, I will add those functions as soon as possible.
Adding Doctrine Model Plugin to your Application:
Its pretty much easy to add this plugin on your new or existing application. I have added the basic codeigniter application structure on github to make it easier to understand. There is only one file that is responsible for this, which is {root}/application/core/MY_DModel.php . Here, two things needed to be remembered carefully:
- If you are adding it to an existing application and your application has a custom ‘MY_Model’ already, then please change this ‘MY_DModel’ class to extend that ‘MY_Model’ instead of ‘CI_Model’.
- To use this from a model, you need to extend this class instead of codeigniter model class or existing MY_Model class. So, if needed, made those changes accordingly as well.
So, as soon as you have complete this , you are done. But of course, you should have Doctrine setup on your application. If you haven’t use doctrine before or new with Doctrine in Codeigniter, feel free to read my another article on using Doctrine ORM with Codeigniter application first.
Basic Usage Strategy For Doctrine Model Plugin:
Using this model plugin is quite simple. First, create a model on which you want to perform the CRUD operation. Lets use the example given on the Github project. I am assuming you have ready entity for this. We are using the ‘DxUsers’ entity in this example which is at {root}/application/models/entities/DxUsers.php location. We will be creating the model named ‘usermodel’ which will perform the basic operations on DxUsers entity. The model class should be something like as follows:
<?php require_once(APPPATH."models/Entities/DxUsers.php"); use \DxUsers; /** * manipulates data and contains data access logics for Enity 'User' * * @final Usermodel * @category models * @author Md. Ali Ahsan Rana * @link https://codesamplez.com */ class Usermodel extends My_DModel { function __construct() { parent::__construct(); $this->init("DxUsers",$this->doctrine->em); } }
This script can be found here: https://github.com/ranacseruet/Doctrine-CRUD-CodeIgniter/blob/master/application/models/usermodel.php
Notice a function call made in constructor of the model class.
$this->init("DxUsers",$this->doctrine->em);
This is also an mandatory part. By this call, you are telling which entity you will be working with and pass the entity manager as well. This is very much helpful in cases, where, you will may like to perform these base functionality for more than one entities in a single model class. You will just need to call this before the common MY_DModel class’s function call.
as soon as its done, we are ready to perform our operations from controllers. See the given example on github on ‘user’ controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * @property Usermodel $usermodel */ class User extends MY_Controller { /** * constructor */ public function __construct() { parent::__construct(); $this->load->model("usermodel"); } /** * @param int $id * user details according to user id */ public function details($id) { try { /*@var $user DxUser */ $user = $this->usermodel->get($id); print_r($user); } catch(Exception $err) { log_message("error", $err->getMessage()); return show_error($err->getMessage()); } } } /* End of file user.php */ /* Location: ./application/controllers/user.php */
This script file can be found here: https://github.com/ranacseruet/Doctrine-CRUD-CodeIgniter/blob/master/application/controllers/user.php
This example only demonstrate the utilization of retrieve/select functionality. Of course, you can use the other methods as well. The following methods can be used at this moment:
//select a single entity by its primary key(usally 'id') $this->usermodel->get($id); //retrieve a number of entities (usually used in paging) as per given criteria/order $this->usermodel->get_by_range($start,$length,$criteria = array(),$orderBy); //return the count for a entity/table $this->usermodel->get_count(); //save an entity $this->usermodel->save($entity); //delete an entity $this->usermodel->delete($ids);
I am looking forward to listen how its working for you and if you have any suggestions, comments, feedback. Happy coding 🙂
Raul Lopez says
cool! thank you
Micheal says
Can explain on to add the phinx plugin in codeigniter 3.1.8