Skip to content

Instantly share code, notes, and snippets.

@cezar62882
Created November 6, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cezar62882/d54f0c7a382bcd9b02ca to your computer and use it in GitHub Desktop.
Save cezar62882/d54f0c7a382bcd9b02ca to your computer and use it in GitHub Desktop.
<?php
/**
* Class Base_Model
*
* Базовая модель
* @property CI_DB_active_record $db
*/
class Base_Model extends CI_Model
{
// Название таблицы
protected $_table = NULL;
// Первичный ключ таблицы
protected $_pk = 'id';
public function __construct()
{
if ( ! isset($this->_table))
{
// Получить название таблицы по названию модели
$this->_table = str_replace('_model', '', strtolower(get_called_class()));
}
parent::__construct();
}
/**
* Вернуть все записи
* @param string $select
* @param null $limit
* @param null $offset
* @param bool $return_array
* @return mixed
*/
public function find_all($select = '*', $limit = NULL, $offset = NULL, $return_array = FALSE)
{
$this->db->select($select);
if ($limit != NULL)
{
$this->db->limit($limit, $offset);
}
$query = $this->db->get($this->_table);
if ($return_array === TRUE)
{
return $query->result_array();
}
else
{
return $query->result(get_called_class());
}
}
/**
* Вернуть все записи, удовлетворяющие условию
* @param string $select
* @param array $condition
* @param null $limit
* @param int $offset
* @param bool $return_array
* @return mixed
*/
public function find_all_by_attributes($select = '*', array $condition, $limit = NULL, $offset = 0, $return_array = FALSE)
{
$this->db->select($select);
foreach ($condition as $attribute => $value)
{
if (is_array($value))
{
$this->db->where_in($attribute, $value);
}
else
{
$this->db->where($attribute, $value);
}
}
if ($limit != NULL)
{
$this->db->limit($limit, $offset);
}
$query = $this->db->get($this->_table);
if ($return_array === TRUE)
{
return $query->result_array();
}
else
{
return $query->result(get_called_class());
}
}
/**
* Получить запись по условию
* @param array $condition условие
* @return object
*/
public function find_by_attributes(array $condition)
{
foreach ($condition as $attribute => $value)
{
$this->db->where($attribute, $value);
}
$model = $this->db->get($this->_table)->row(0, get_called_class());
if (empty($model))
{
return NULL;
}
return $model;
}
/**
* Получить модель по pk
*
* @param $pk
* @return object
*/
public function find($pk)
{
$model = $this->db->get_where($this->_table, array($this->_pk => $pk))->row(0, get_called_class());
if (empty($model))
{
return NULL;
}
return $model;
}
/**
* Ищет метод у модели, начинающейся на get у несуществующего свойства
* Например нам нужно получить image_path у класса Banners_model, но данное свойство не хранится в таблице и разное в зависимости от
* параметра type, поэтому необходимо лишь реализовать метод get_image_path() у модели Banners_model
*
* @param $name
* @return mixed
*/
public function __get($name)
{
if (method_exists($this, 'get_' . $name))
{
return call_user_func(array($this, 'get_' . $name));
}
else
{
return parent::__get($name);
}
}
/**
* Подсчитать количество записей по условию
* @param array $condition условие
* @return int
*/
public function count(array $condition = array())
{
foreach ($condition as $attribute => $value)
{
$this->db->where($attribute, $value);
}
return $this->db->count_all_results($this->_table);
}
/**
* Обновить запись по pk
* @param $pk
* @param array $attributes
* @return object
*/
public function update($pk, array $attributes)
{
return $this->db->update($this->_table, $attributes, array($this->_pk => $pk));
}
public function update_by_attributes($attributes, $condition)
{
return $this->db->update($this->_table, $attributes, $condition);
}
/**
* Удалить запись по pk
* @param $pk
* @return object
*/
public function delete($pk)
{
return $this->db->delete($this->_table, array($this->_pk => $pk));
}
/**
* Удалить записи по условию
* @param mixed $condition условие
* @return object
*/
public function delete_by_attributes($condition)
{
return $this->db->delete($this->_table, $condition);
}
/**
* Добавить новую запись
* @param array $attributes
* @return object
*/
public function insert(array $attributes)
{
return $this->db->insert($this->_table, $attributes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment