1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/redraiment-phpActiveRecord

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
В этом репозитории не указан файл с открытой лицензией (LICENSE). При использовании обратитесь к конкретному описанию проекта и его зависимостям в коде.
Клонировать/Скачать
record.php 2.8 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
redraiment Отправлено 29.04.2016 12:20 cae5e60
<?php
require_once('table.php');
class Record {
private $table;
private $values;
private $cached;
function __construct($table, $values) {
$this->table = $table;
$this->values = $values;
$this->cached = [];
}
public function columnNames() {
return array_keys($this->values);
}
public function __get($name) {
$values = $this->values;
$table = $this->table;
$relations = $table->relations;
$hooks = $table->hooks;
$value = null;
if (isset($values[$name])) {
$value = $values[$name];
} elseif (isset($this->cached[$name])) {
$value = $this->cached[$name];
} elseif (isset($relations[$name])) {
$relation = $relations[$name];
$target = $relation->target;
$alias = ($relation->alias === null)? $table->name: $relation->alias;
$active = $table->db->$target;
$active->join($relation->assoc($table->name, $alias, $values['id']));
if ($relation->ancestor && !$relation->cross) {
$active->constrain($relation->foreignKey, $values['id']);
}
$value = $relation->onlyOne? $active->first(): $active;
$this->cached[$name] = $value;
}
$key = 'get_' . $name;
if (isset($hooks[$key])) {
$value = $hooks[$key]($this, $value);
} elseif (isset($hooks['get_*'])) {
$value = $hooks['get_*']($this, $name, $value);
}
return (is_numeric($value) && preg_match('/^[1-9]/', $value))? ($value + 0): $value;
}
public function __set($name, $value) {
$name = parseKeyParameter($name);
$hooks = $this->table->hooks;
$key = 'set_' . $name;
if (isset($hooks[$key])) {
$value = $hooks[$key]($this, $value);
}
$this->values[$name] = $value;
return $this;
}
public function __call($name, $arguments) {
$hooks = $this->table->hooks;
$key = 'call_' . $name;
if (isset($hooks[$key])) {
return $hooks[$key]($this, ...$arguments);
} elseif (isset($hooks['call_*'])) {
return $hooks['call_*']($this, $name, $arguments);
}
return null;
}
public function save() {
$this->table->update($this);
return $this;
}
public function update() {
$args = func_get_args();
for ($i = 0; $i < func_num_args(); $i += 2) {
$this->__set($args[$i], $args[$i + 1]);
}
return $this->save();
}
public function destroy() {
$this->table->delete($this);
}
public function __toString() {
return implode("\n", array_map(function($key) {
return $key . ' = ' . $this->values[$key];
}, array_keys($this->values)));
}
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/redraiment-phpActiveRecord.git
git@api.gitlife.ru:oschina-mirror/redraiment-phpActiveRecord.git
oschina-mirror
redraiment-phpActiveRecord
redraiment-phpActiveRecord
master