122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace dominion\db;
|
|
|
|
use Yii;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class ActiveRecord extends \yii\db\ActiveRecord
|
|
{
|
|
protected static $tableSchemaCache = [];
|
|
|
|
/**
|
|
* этот класс позволяет ускорить работу за счет хранения структуры бд
|
|
*/
|
|
public static function getTableSchema()
|
|
{
|
|
if(!isset(self::$tableSchemaCache[static::tableName()]))
|
|
{
|
|
self::$tableSchemaCache[static::tableName()] = parent::getTableSchema();
|
|
}
|
|
return self::$tableSchemaCache[static::tableName()];
|
|
}
|
|
|
|
/**
|
|
* этот класс позволяет ускорить работу за счет отказа от получения списка атрибутов из схемы бд
|
|
* /
|
|
public function attributes()
|
|
{
|
|
return array_keys($this->attributeLabels());
|
|
}*/
|
|
|
|
public function init()
|
|
{
|
|
}
|
|
|
|
public function insertUpdate($runValidation = true, $attributes = null)
|
|
{
|
|
if ($runValidation && !$this->validate($attributes)) {
|
|
Yii::info('Model not inserted due to validation error.', __METHOD__);
|
|
return false;
|
|
}
|
|
|
|
if (!$this->isTransactional(self::OP_INSERT)) {
|
|
return $this->insertUpdateInternal($attributes);
|
|
}
|
|
|
|
$transaction = static::getDb()->beginTransaction();
|
|
try {
|
|
$result = $this->insertUpdateInternal($attributes);
|
|
if ($result === false) {
|
|
$transaction->rollBack();
|
|
} else {
|
|
$transaction->commit();
|
|
}
|
|
|
|
return $result;
|
|
} catch (\Exception $e) {
|
|
$transaction->rollBack();
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
$transaction->rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
|
|
protected function insertUpdateInternal($attributes = null)
|
|
{
|
|
if (!$this->beforeSave(true)) {
|
|
return false;
|
|
}
|
|
$values = $this->getDirtyAttributes($attributes);
|
|
|
|
|
|
if (($primaryKeys = $this->schemaInsertUpdate(static::tableName(), $values)) === false) {
|
|
return false;
|
|
}
|
|
foreach ($primaryKeys as $name => $value) {
|
|
$id = static::getTableSchema()->columns[$name]->phpTypecast($value);
|
|
$this->setAttribute($name, $id);
|
|
$values[$name] = $id;
|
|
}
|
|
|
|
$changedAttributes = array_fill_keys(array_keys($values), null);
|
|
$this->setOldAttributes($values);
|
|
$this->afterSave(true, $changedAttributes);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function schemaInsertUpdate($table, $columns)
|
|
{
|
|
$db = static::getDb();
|
|
foreach ($this->attributes() as $itemColumn) {
|
|
$column = $db->getSchema()->quoteColumnName($itemColumn);
|
|
$onDuplicateKeyValues[] = $column . ' = VALUES(' . $column . ')';
|
|
}
|
|
|
|
$command = $db->createCommand()->insert($table, $columns);
|
|
$sql = $command->getRawSql();
|
|
$sql .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $onDuplicateKeyValues);;
|
|
|
|
if (!$db->createCommand($sql)->execute()) {
|
|
return false;
|
|
}
|
|
$tableSchema = $this->getTableSchema($table);
|
|
$result = [];
|
|
foreach ($tableSchema->primaryKey as $name) {
|
|
if ($tableSchema->columns[$name]->autoIncrement) {
|
|
$result[$name] = $this->getLastInsertID($tableSchema->sequenceName);
|
|
break;
|
|
}
|
|
|
|
$result[$name] = isset($columns[$name]) ? $columns[$name] : $tableSchema->columns[$name]->defaultValue;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
}
|