• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Database / Doctrine Model Plugin For Codeigniter

Doctrine Model Plugin For Codeigniter

November 21, 2012 by Rana Ahsan 3 Comments

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:

  1. 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’.
  2. 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 http://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 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Database, Development Tagged With: codeigniter, doctrine, php, plugin

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Raul Lopez says

    September 18, 2015 at 4:20 pm

    cool! thank you

    Reply
  2. Micheal says

    May 29, 2018 at 1:29 am

    Can explain on to add the phinx plugin in codeigniter 3.1.8

    Reply

Trackbacks

  1. Codeigniter Bundle With Popular Libraries And Best PracticesCodeSamplez.com says:
    December 21, 2012 at 8:06 am

    […] CRUD Functionality for models: My another doctrine model class for codeigniter, which is being integrated here to give you ready made crud functionality for new […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • Beginning Codeigniter Application Development
    Beginning Codeigniter Application Development
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023