Created
April 24, 2017 21:13
-
-
Save zeromodule/fd367e246499574d084681431b894e28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//класс для построения на его основе классов для доступа | |
//к объектам данных из БД (блоги, записи, комментарии, пользователи, ...). | |
//Предоставляет методы для работы как с одиночными объектами или их свойствами, | |
//так и с их группами. | |
//не абстрактный, т.к. иногда достаточно его экземпляра. | |
class DataObjects{ | |
/** | |
* Имя таблицы | |
* | |
* @var string | |
*/ | |
protected $table; | |
/** | |
* ID объекта в таблице | |
* | |
* @var integer | |
*/ | |
protected $id; | |
/** | |
* Объект класса DBHandle | |
* | |
* @var DBHandle | |
*/ | |
private $db; | |
/** | |
* Конструктор класса. | |
* | |
* @param integer $id | |
* @param DBHandle $db | |
*/ | |
function __construct($id=false, $db=false){ | |
if($id && is_numeric($id)) $this->id = $id; | |
$this->db = $db ? $db : Base::initDB(); | |
if($id && $this->table) | |
{ | |
$data = $this->getPropertiesByIds(); | |
foreach($this as $key=>$value) | |
{ | |
if(isset($data[0]->$key) && !isset($this->$key)) $this->$key = $data[0]->$key; | |
} | |
} | |
} | |
function setTable($table) | |
{ | |
$this->table = $table; | |
} | |
/** | |
* Перегрузка стандартного метода. | |
* Возвращает значение свойства объекта. | |
* | |
* @param string $name | |
* @return string | |
*/ | |
function __get($name){ | |
if(!$this->id) return false; | |
if(!isset($this->$name)) $this->$name = current($this->getPropertiesByIds($name, $this->id))->$name; | |
return $this->$name; | |
} | |
/** | |
* Возвращает массив (список, справочник) всех значений интересующего поля/полей | |
* | |
* @param string $table | |
* @param array $properties | |
* @return array | |
*/ | |
public function getList($table=false, $properties=false){ | |
if(!$table) $table = $this->table; | |
if($properties){ | |
if(!is_array($properties)) $properties = array($properties); | |
foreach($properties as $k=>$v) $properties[$k] = "`" . $v . "`"; | |
$fields_str = implode(',', $properties); | |
}else{ | |
$fields_str = "*"; | |
} | |
return $this->db->fetchObject_arr("SELECT " . $fields_str . " FROM `" . $table . "`"); | |
} | |
/** | |
* Возвращает значения интересующих свойств объекта/объектов | |
* | |
* @param array $properties | |
* @param array $object_ids | |
* @param string $table | |
* @return array | |
*/ | |
public function getPropertiesByIds($properties=false, $object_ids=false, $table=false){ | |
if(!$object_ids) $object_ids = $this->id; | |
if(!is_array($object_ids)) $object_ids = array($object_ids); | |
if(!$table) $table = $this->table; | |
foreach($object_ids as $k=>$v) $object_ids[$k] = "'" . $v . "'"; | |
if($properties){ | |
if(!is_array($properties)) $properties = array($properties); | |
foreach($properties as $k=>$v) $properties[$k] = "`" . $v . "`"; | |
$fields_str = implode(',', $properties); | |
}else{ | |
$fields_str = "*"; | |
} | |
return $this->db->fetchObject_arr("SELECT " . $fields_str . " FROM `" . $table . "` WHERE id IN (" . implode(',', $object_ids) . ")"); | |
} | |
/** | |
* Обновляет необходимые свойства объекта/группы объектов | |
* | |
* @param array $object_ids | |
* @param array $properties | |
* @param string $table | |
* @return boolean | |
*/ | |
public function updatePropertiesByIds($object_ids=false, $properties, $table=false){ | |
if(!is_array($properties)) return false; | |
if(!$object_ids) $object_ids = $this->id; | |
if(!is_array($object_ids)) $object_ids = array($object_ids); | |
if(!$table) $table = $this->table; | |
foreach($object_ids as $k=>$v) $object_ids[$k] = "'" . $v . "'"; | |
foreach($properties as $k=>$v) $fields[] = "`" . $k . "`='" . $v ."'"; | |
$fields_str = implode(',', $fields); | |
if($this->db->query("UPDATE `" . $table . "` SET " . $fields_str . " WHERE id IN (" . implode(',', $object_ids) . ")")) return true; | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment