• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple ORM

#1
[eluser]Daeli[/eluser]
Hey guys, here is a little contribution from me:

This Class called MasterModel provides a very basic and extensible ORM. It still uses the CI Active Record functionallity but with some juice added. With this Model you are able to map Results of your DB-Query into so called DataObjects. These objects hold the data on the one side but also are able to hold functions to manipulate the result.

Here is a little usage example:

Code:
$this->db->where('id>', 10);
    $data = $this->mastermodel->get('table')->result();
    foreach($data as $result){
        echo $result->get_formatted_date();
    }

Here you can see that each of these results can have functions and so on.

In the following code you can see an example DataObject Class
Code:
Class mdl_message extends DataObject{
    public $id;
    public $message_from;
    public $message_to;
    public $message_subject;
    public $message_text;
    public $message_date;
    
    function get_message_date($format = null){
        $ci =& get_instance();
        
        if($format === null)
            $format = $ci->lang->line('date_format');
    
        if($format === null)
            $format = "d.m Y H:i";
            
        $ci->load->helper('date');
        return date($format, mysql_to_unix($this->message_date));
    }
}
Some Notes to the code above:
Every DataObject Class must extend the DataObject Class and must have mdl_ in its class name. The MasterModel needs this to find out if its the correct DataObject for the result associated with it. Here you can add any functionality.

The name of the model must have the following format: mdl_<your table name>

I hope some of you understood this Wink
Iam not dropping this into Ignited Code Board because i need your feedback and this class is very very basic. Please let me know Smile

Here is the code: MasterModel

#2
[eluser]Daeli[/eluser]
Did anyone test it yet?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.