[eluser]Sam Dark[/eluser]
Another one based on Developer13's AutoModel:
Code:
<?php
/**
* AutoModel PHP5
* @version 0.1
* @author Initial idea and code by Developer13 (http://ellislab.com/forums/member/41286/)
* @author Sam Dark
*/
class AutoModel extends Model {
public $return_method = 'object';
protected $table = null;
//id is most commonly used for primary key
protected $primary_key = 'id';
//if array - define alias
//if no . in name - use current table name
protected $fields = array();
/**
* Behaviors could automate your models even more. Currently there is only one supported.
* TimestampableBehavior. Updates created_on and updated_on in model table with
* current unix timestamp.
*
* Usage:
* $this->addBehavior(new TimestampableBehavior());
*/
private $behaviors = array();
/**
* $joins = array(
* 'table_name1' => 'table_name1.id = id',
* 'table_name2' => array('table_name2.id = id', 'left'),
* )
*
* @see $this->db->join(); in Active Record section in user guide for join types
*/
protected $joins = array();
function __construct() {
parent::__construct();
}
function addBehavior(AutomodelBehavior $behavior){
$this->behaviors[] = $behavior;
}
/**
* If there is no table name add this model table name.
*/
private function update_fields(){
foreach ($this->fields as &$field){
if(!stristr($field, '.')){
$field = $this->table.'.'.$field;
}
}
}
/**
* Get records from model.
*
* @param array|string $where
* @param int|array $limit
* @return array
*
* @see /database/active_record.html
* You can use methods 3 and 4 from User Guide on $this->db->where().
*
* For $limit you can use int to produce LIMIT n
* or array(limit, offset) to produce LIMIT limit, offset
*/
function get($where = null, $limit = null, $protect = true) {
$this->update_fields();
$this->db->select($this->fields);
if (!empty($where)){
$this->db->where($where, null, $protect);
}
$this->perform_join();
if(is_array($limit)){
$query = $this->db->get($this->table, $limit[0], $limit[1]);
}
else {
$query = $this->db->get($this->table, $limit);
}
if ($this->return_method == 'object') {
if ($limit == 1) {
return $query->row();
}
else {
return $query->result();
}
}
elseif ($this->return_method == 'array') {
if ($limit == 1) {
return $query->row_array();
}
else {
return $query->result_array();
}
}
}
/**
* Saves data to the model.
*
* @param array|string $where if specified, update query is generated, else insert query
* @see also $this->get description
* @param array $db_array
* //TODO: save data to joined models?
* You can specify data to save manually instead of getting it from POST. To do it use
* second parameter.
*/
function save($where = null, $data = null) {
if(empty($data)) $data = $this->db_array();
$this->db->set($data);
if (!empty($where)) {
if(!empty($this->behaviors)){
foreach ($this->behaviors as $behavior){
$behavior->on_update($this);
}
}
$this->db->where($where);
$this->db->update($this->table);
}
else {
if(!empty($this->behaviors)){
foreach ($this->behaviors as $behavior){
$behavior->on_insert($this);
}
}
$this->db->insert($this->table);
}
}
/**
* Deletes model record based on condition.
* //TODO: delete data from joined models?
*
* @param array|string $where
*/
function delete($where) {
if(!empty($this->behaviors)){
foreach ($this->behaviors as $behavior){
$behavior->on_delete($this);
}
}
$this->db->where($where);
$this->db->delete($this->table);
}
/**
* Adds joins to the query based on $this->join.
*/
private function perform_join() {
if (!empty($this->joins)) {
foreach ($this->joins as $table => $condition) {
if(is_array($condition)){
$this->db->join($table, $condition[0], $condition[1]);
}
else {
$this->db->join($table, $condition);
}
}
}
}
/**
* Fills array for save from POST.
* Form must have the same names as specified in the model definition.
*
* @return array
*/
private function db_array() {
$db_array = array();
foreach ($this->fields as $field) {
$field = substr($field, strpos($field, '.') + 1);
if (isset($_POST[$field])) {
$db_array[$field] = $this->input->post($field);
}
}
return ($db_array);
}
}