Welcome Guest, Not a member yet? Register   Sign In
Yet another ORM
#1

[eluser]philpalmieri[/eluser]
Hi,

After toying with my models, and repeating to much code, i am writing a simple (very simple) ORM style model.

It's not ment to be for everyone, or to do everything, but it seems to be working at the moment, and if there is any interest i will publich the completed code when i finish it.

First step, create a model call Ezmodel (what im calling it for now)
Code:
/* models/ezmodel.php */
<?php
class Ezmodel extends Model {
  protected $table = "";
  protected $key = "id";
  protected $has_many = array(); // ("table", "table", "table");
  public $result = array(); //
  
  public function __construct()
  {
    parent::Model();
  }
  
  public function getone($id)
  {
    if($id > 0) {
      $this->db->where($this->key, $id);
      $this->db->limit("1");
      $res = $this->db->get($this->table);
      if($res->num_rows() == 1) {
        $this->result[]['r'] = $res->row();
        $this->get_what_has();
      }
    }
  }
  
  public function getmany($args, $limit=20)
  {
    foreach($args as $col=>$arg) {
      $this->db->where($col, $arg);
      $this->db->limit($limit);
      $res = $this->db->get($this->table);
      if($res->num_rows() > 0) {
        foreach($res->result() as $row) {
          $this->result[]['r'] = $row;
        }
        $this->get_what_has();
      }
    }
  }
  
  public function saveone($vals, $id=NULL)
  {
    
  }
  
  public function savemany($vals)
  {
    
  }
  
  ////
  
  private function get_what_has()
  {
    if(count($this->result) > 0) {
      foreach($this->result as $num=>$row) {
        foreach($this->has_many as $table) {
          $this->db->where($this->table."_".$this->key, $row['r']->{$this->key});
          $this->db->order_by($this->key, "DESC");
          $res = $this->db->get($table);
          if($res->num_rows() > 0) {
            foreach($res->result() as $orow) {
              $this->result[$num][$table][] = $orow;
            }
          }
        }
      }
    }
  }
  
}
?>

Then, build your own model that extends ezmodel only defining the table name and the has many's...
Code:
/* /models/patient.php */
<?php
class Patient extends Ezmodel {
  public $table = "patient";
  public $has_many = array("phone");
}
?>

Now, your controller needs to load model ezmodel so Model is loaded, then load your model
Code:
<?php

class Dashboard extends Controller {

    public function __construct()
    {
        parent::Controller();
        $this->load->model("ezmodel", "ezmodel", TRUE);
        $this->load->model("patient", "patient", TRUE);
    }
    
    public function index()
    {
      $this->patient->getone("1");
        $this->load->view('dashboard/index');
    }
}
?>

Now, your view has access to the patient->results array
Code:
<?=$this->load->view("global/header");?>

<ul>
  &lt;? foreach($this->patient->result as $patient): ?&gt;
    <li>
      &lt;?=$patient['r']->last_name;?&gt;, &lt;?=$patient['r']->first_name;?&gt;
      &lt;?=$patient['phone'][0]->type;?&gt;: &lt;?=$patient['phone'][0]->number;?&gt;
    </li>
  &lt;? endforeach; ?&gt;
</ul>

&lt;?=$this->load->view("global/footer");?&gt;

Now this assumes a few things:
1, each table has an auto-increment key
2, each table that belongs to another has a reference in itself to the ownere.. is the phone table has a row called patient_id

Let me know if anyone else sees this as useful, i still need to build out the CRUD functionality and load it up with exception handling as well as wrapping everything in more security - but its a start.
#2

[eluser]nmweb[/eluser]
You put a query in a loop. This can produce some pretty bad results when you have a table with say 100.000 records. I just glanced over the code so maybe I'm wrong Smile
#3

[eluser]philpalmieri[/eluser]
Hi nmweb,

Thats why i put a limiter on the get many, i do not foresee this being used as a reporting engine or anything else, more for in our applications where i dont want to have to write all the CRUD stuff over and over in each model. My vision of this is it maybe returning 30 or so records and paginating them if there is more.

Phil




Theme © iAndrew 2016 - Forum software by © MyBB