[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>
<? foreach($this->patient->result as $patient): ?>
<li>
<?=$patient['r']->last_name;?>, <?=$patient['r']->first_name;?>
<?=$patient['phone'][0]->type;?>: <?=$patient['phone'][0]->number;?>
</li>
<? endforeach; ?>
</ul>
<?=$this->load->view("global/footer");?>
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.