Files
kiri-core/Database/ActiveRecord.php
T

451 lines
9.0 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-11-10 11:36:39 +08:00
2020-08-31 12:38:32 +08:00
namespace Database;
use Exception;
use Database\Base\BaseActiveRecord;
use Snowflake\Core\ArrayAccess;
2020-09-10 16:22:31 +08:00
use Snowflake\Error\Logger;
2020-12-17 14:09:14 +08:00
use Snowflake\Exception\ComponentException;
2020-09-10 16:22:31 +08:00
use Snowflake\Snowflake;
2020-08-31 12:38:32 +08:00
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
2020-09-25 15:37:33 +08:00
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
2020-08-31 12:38:32 +08:00
/**
* Class Orm
* @package Database
*
* @property $attributes
* @property-read $oldAttributes
* @method beforeSearch($model)
*/
class ActiveRecord extends BaseActiveRecord
{
2020-11-17 18:48:26 +08:00
const DECR = 'decr';
const INCR = 'incr';
/**
* @return array
*/
2020-12-17 14:09:14 +08:00
public function rules(): array
2020-11-17 18:48:26 +08:00
{
return [];
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function increment(string $column, int $value): bool|ActiveRecord
2020-11-17 18:48:26 +08:00
{
if (!$this->mathematics(self::INCR, [$column => $value])) {
return false;
}
$this->{$column} += $value;
return $this->refresh();
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function decrement(string $column, int $value): bool|ActiveRecord
2020-11-17 18:48:26 +08:00
{
if (!$this->mathematics(self::DECR, [$column => $value])) {
return false;
}
$this->{$column} -= $value;
return $this->refresh();
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function increments(array $columns): bool|static
2020-11-17 18:48:26 +08:00
{
if (!$this->mathematics(self::INCR, $columns)) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key += $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function decrements(array $columns): bool|static
2020-11-17 18:48:26 +08:00
{
if (!$this->mathematics(self::DECR, $columns)) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key -= $attribute;
}
return $this;
}
/**
2020-12-17 14:09:14 +08:00
* @param array $condition
* @param array $attributes
* @return mixed
* @throws ComponentException
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public static function findOrCreate(array $condition, array $attributes = []): mixed
2020-11-17 18:48:26 +08:00
{
2020-11-18 15:05:33 +08:00
$select = static::find()->where($condition)->first();
2020-11-17 18:48:26 +08:00
if (!empty($select)) {
return $select;
}
if (empty($attributes)) {
return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
2020-11-18 15:05:33 +08:00
$select = new static();
2020-11-17 18:48:26 +08:00
$select->attributes = $attributes;
2020-11-17 18:48:55 +08:00
if (!$select->save()) {
2020-11-17 18:50:19 +08:00
throw new Exception($select->getLastError());
2020-11-17 18:48:26 +08:00
}
return $select;
}
/**
* @param $action
* @param $columns
* @param array $condition
* @return array|bool|int|string|null
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function mathematics($action, $columns, $condition = []): int|bool|array|string|null
2020-11-17 18:48:26 +08:00
{
if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
return static::getDb()->createCommand()
->mathematics(self::getTable(), [$action => $columns], $condition)
->exec();
}
/**
* @param $column
* @param $value
* @param array $condition
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public static function incrAll($column, $value, $condition = []): bool
2020-11-17 18:48:26 +08:00
{
return static::getDb()->createCommand()
->mathematics(self::getTable(), [self::INCR => [$column, $value]], $condition)
->exec();
}
/**
* @param $column
* @param $value
* @param array $condition
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public static function decrAll($column, $value, $condition = []): bool
2020-11-17 18:48:26 +08:00
{
return static::getDb()->createCommand()
->mathematics(self::getTable(), [self::DECR => [$column, $value]], $condition)
->exec();
}
/**
* @param array $attributes
2020-12-17 15:20:43 +08:00
* @return ActiveRecord|bool
* @throws Exception
2020-11-17 18:48:26 +08:00
*/
2020-12-17 15:20:43 +08:00
public function update(array $attributes): static|bool
2020-11-17 18:48:26 +08:00
{
return $this->save($attributes);
}
/**
* @param array $params
* @param array $condition
* @return bool|static
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public static function insertOrUpdate(array $params, array $condition): bool|static
2020-11-17 18:48:26 +08:00
{
2020-11-17 18:50:19 +08:00
$first = static::findOrCreate($condition, $params);
2020-11-17 18:48:26 +08:00
$first->attributes = $params;
if (!$first->save()) {
return false;
}
return $first;
}
/**
* @param array $data
* @return bool
* @throws Exception
*/
public static function batchInsert(array $data): bool
{
/** @var static $class */
$class = Snowflake::createObject(static::className());
if (empty($data)) {
return $class->addError('Insert data empty.', 'mysql');
}
return $class::find()->batchInsert($data);
}
/**
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function delete(): bool
2020-11-17 18:48:26 +08:00
{
$conditions = $this->_oldAttributes;
if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql');
}
$primary = $this->getPrimary();
if (!empty($primary)) {
2020-12-17 14:17:03 +08:00
$sul = static::deleteByCondition([$primary => $this->getAttribute($primary)]);
2020-11-17 18:48:26 +08:00
} else {
2020-12-17 14:17:03 +08:00
$sul = static::deleteByCondition($conditions);
2020-11-17 18:48:26 +08:00
}
if (!$sul) {
return false;
}
if (method_exists($this, 'afterDelete')) {
$this->afterDelete();
}
return true;
}
/**
* @param $condition
* @param array $attributes
*
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public static function batchUpdate($condition, $attributes = []): bool
2020-11-17 18:48:26 +08:00
{
$condition = static::find()->where($condition);
return $condition->batchUpdate($attributes);
}
/**
* @param $condition
* @param array $attributes
*
2020-12-17 14:09:14 +08:00
* @return array|Collection
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public static function findAll($condition, $attributes = []): array|Collection
2020-11-17 18:48:26 +08:00
{
$query = static::find()->where($condition);
if (!empty($attributes)) {
$query->bindParams($attributes);
}
return $query->all();
}
/**
* @param $data
2020-12-17 14:09:14 +08:00
* @return mixed
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function resolveObject($data): mixed
2020-11-17 18:48:26 +08:00
{
if (is_numeric($data) || !is_string($data)) {
return $data;
}
if (!method_exists($this, $data)) {
return $data;
}
2020-11-26 10:49:59 +08:00
$resolve = $this->{$data}();
if ($resolve instanceof Collection) {
return $resolve->toArray();
}
if ($resolve instanceof ActiveRecord) {
return $resolve->toArray();
}
if (is_object($resolve)) {
return get_object_vars($resolve);
}
return $resolve;
2020-11-17 18:48:26 +08:00
}
/**
2020-12-17 14:09:14 +08:00
* @return array
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function toArray(): array
2020-11-17 18:48:26 +08:00
{
$data = [];
foreach ($this->_attributes as $key => $val) {
$data[$key] = $this->getAttribute($key);
}
return array_merge($data, $this->runRelate());
}
/**
* @return array
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function runRelate(): array
2020-11-17 18:48:26 +08:00
{
$relates = [];
if (empty($this->_relate)) {
return $relates;
}
foreach ($this->_relate as $key => $val) {
$relates[$key] = $this->resolveObject($val);
}
return $relates;
}
/**
2020-12-17 14:09:14 +08:00
* @param string $modelName
2020-11-17 18:48:26 +08:00
* @param $foreignKey
* @param $localKey
2020-12-17 14:09:14 +08:00
* @return HasOne
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne
2020-11-17 18:48:26 +08:00
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
return new HasOne($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
2020-12-17 14:09:14 +08:00
* @return HasCount
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function hasCount($modelName, $foreignKey, $localKey): HasCount
2020-11-17 18:48:26 +08:00
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
return new HasCount($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
2020-12-17 14:09:14 +08:00
* @return HasMany
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function hasMany($modelName, $foreignKey, $localKey): HasMany
2020-11-17 18:48:26 +08:00
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
2020-12-17 14:09:14 +08:00
* @return HasMany
2020-11-17 18:48:26 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function hasIn($modelName, $foreignKey, $localKey): HasMany
2020-11-17 18:48:26 +08:00
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation);
}
/**
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function afterDelete(): bool
2020-11-17 18:48:26 +08:00
{
if (!$this->hasPrimary()) {
return TRUE;
}
$value = $this->getPrimaryValue();
if (empty($value)) {
return TRUE;
}
return TRUE;
}
/**
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function beforeDelete(): bool
2020-11-17 18:48:26 +08:00
{
if (!$this->hasPrimary()) {
return TRUE;
}
$value = $this->getPrimaryValue();
if (empty($value)) {
return TRUE;
}
return TRUE;
}
2020-08-31 12:38:32 +08:00
}