In an earlier tutorial, I showed how we could easily perform the CRUD operations using Doctrine ORM. However, as a lot of developers use doctrine ORM as part of their favourite Codeigniter 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, and delete operations.
The Plugin Script:
As a result, I have written an extended core codeigniter class, ‘MY_DModel'(‘D’ stands for doctrine), which utilizes doctrine ORM and performs the functionality of select, insert, update, delete, etc. general operations. So, for these regular operations, you can use these ready functions instead of writing your own. You can find this project on GitHub on the following URL:
I have only implemented the most regular operations. It is possible that I 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:
It’s pretty much easy to add this plugin to 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 need to be remembered carefully:
If you are adding it to an existing application and your application already has a custom ‘MY_Model’, 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 the codeigniter model class or the existing MY_Model class. So, if needed, make those changes accordingly as well.
So, as soon as you have completed this, you are done. But of course, you should have Doctrine set up for your application. If you haven’t used Doctrine before or are new to Doctrine in Codeigniter, feel free to read my other article on using Doctrine ORM with the 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. Let’s use the example given on the GitHub project. I am assuming you have a 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:
<?phprequire_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
*/classUsermodelextendsMy_DModel{
function__construct(){
parent::__construct();
$this->init("DxUsers",$this->doctrine->em);
}
}Code language:HTML, XML(xml)
This is also an mandatory part. By this call, you are telling which entity you will be working with and passing the entity manager as well. This is very helpful in cases where you may like to perform these base functionalities for more than one entity in a single model class. You will just need to call this before the common MY_DModel class’s function call.
As soon as it’s done, we will be ready to perform our operations from the controllers. See the given example on GitHub on ‘user’ controller:
<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @property Usermodel $usermodel
*/classUserextendsMY_Controller{
/**
* constructor
*/publicfunction__construct(){
parent::__construct();
$this->load->model("usermodel");
}
/**
* @param int $id
* user details according to user id
*/publicfunctiondetails($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 */Code language:HTML, XML(xml)
This example only demonstrates 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);Code language:PHP(php)
I am looking forward to listening to how it’s working for you and if you have any suggestions, comments, or feedback. Happy coding 🙂
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Raul Lopez says
cool! thank you
Micheal says
Can explain on to add the phinx plugin in codeigniter 3.1.8