This commit is contained in:
2021-04-25 11:27:13 +08:00
parent 77a6445eea
commit be8faa5cb0
6 changed files with 374 additions and 371 deletions
+360 -358
View File
@@ -13,6 +13,7 @@ namespace Database;
use Database\Base\BaseActiveRecord;
use Database\Traits\HasBase;
use Exception;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Exception\NotFindClassException;
@@ -32,415 +33,416 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a
class ActiveRecord extends BaseActiveRecord
{
const DECR = 'decr';
const INCR = 'incr';
const DECR = 'decr';
const INCR = 'incr';
/**
* @return array
*/
public function rules(): array
{
return [];
}
/**
* @return array
*/
public function rules(): array
{
return [];
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
public function increment(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics([$column => $value], '+')) {
return false;
}
$this->{$column} += $value;
return $this->refresh();
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
public function increment(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics([$column => $value], '+')) {
return false;
}
$this->{$column} += $value;
return $this->refresh();
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
public function decrement(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics([$column => $value], '-')) {
return false;
}
$this->{$column} -= $value;
return $this->refresh();
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
public function decrement(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics([$column => $value], '-')) {
return false;
}
$this->{$column} -= $value;
return $this->refresh();
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
public function increments(array $columns): bool|static
{
if (!$this->mathematics($columns, '+')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key += $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
public function increments(array $columns): bool|static
{
if (!$this->mathematics($columns, '+')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key += $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
public function decrements(array $columns): bool|static
{
if (!$this->mathematics($columns, '-')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key -= $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
public function decrements(array $columns): bool|static
{
if (!$this->mathematics($columns, '-')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key -= $attribute;
}
return $this;
}
/**
* @param array $condition
* @param array $attributes
* @return bool|ActiveRecord
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public static function findOrCreate(array $condition, array $attributes = []): bool|static
{
$logger = Snowflake::app()->getLogger();
/**
* @param array $condition
* @param array $attributes
* @return bool|ActiveRecord
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public static function findOrCreate(array $condition, array $attributes = []): bool|static
{
$logger = Snowflake::app()->getLogger();
/** @var static $select */
$select = static::find()->where($condition)->first();
if (!empty($select)) {
return $select;
}
if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
$select = self::getModelClass();
$select->attributes = $attributes;
if (!$select->save()) {
return $logger->addError($select->getLastError(), 'mysql');
}
return $select;
}
/** @var static $select */
$select = static::find()->where($condition)->first();
if (!empty($select)) {
return $select;
}
if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
$select = self::getModelClass();
$select->attributes = $attributes;
if (!$select->save()) {
return $logger->addError($select->getLastError(), 'mysql');
}
return $select;
}
/**
* @param array $condition
* @param array $attributes
* @return bool|static
* @throws Exception
*/
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{
$logger = Snowflake::app()->getLogger();
if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
/** @var static $select */
$select = static::find()->where($condition)->first();
if (empty($select)) {
$select = self::getModelClass();
}
$select->attributes = $attributes;
if (!$select->save()) {
return $logger->addError($select->getLastError(), 'mysql');
}
return $select;
}
/**
* @param array $condition
* @param array $attributes
* @return bool|static
* @throws Exception
*/
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{
$logger = Snowflake::app()->getLogger();
if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
/** @var static $select */
$select = static::find()->where($condition)->first();
if (empty($select)) {
$select = self::getModelClass();
}
$select->attributes = $attributes;
if (!$select->save()) {
return $logger->addError($select->getLastError(), 'mysql');
}
return $select;
}
/**
* @return static
* @throws Exception
*/
private static function getModelClass(): static
{
$className = get_called_class();
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
return $channel->pop($className, function () use ($className) {
return new $className();
});
}
/**
* @return static
* @throws Exception
*/
#[Pure] private static function getModelClass(): static
{
return new static();
$className = get_called_class();
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
return $channel->pop($className, function () use ($className) {
return new $className();
});
}
/**
* @param $action
* @param $columns
* @param array $condition
* @return array|bool|int|string|null
* @throws Exception
*/
private function mathematics($columns, $action, $condition = []): int|bool|array|string|null
{
if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
/**
* @param $action
* @param $columns
* @param array $condition
* @return array|bool|int|string|null
* @throws Exception
*/
private function mathematics($columns, $action, $condition = []): int|bool|array|string|null
{
if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
$activeQuery = static::find()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
if (is_bool($create)) {
return false;
}
return static::getDb()->createCommand($create[0], $create[1])->exec();
}
$activeQuery = static::find()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
if (is_bool($create)) {
return false;
}
return static::getDb()->createCommand($create[0], $create[1])->exec();
}
/**
* @param array $fields
* @return ActiveRecord|bool
* @throws Exception
*/
public function update(array $fields): static|bool
{
return $this->save($fields);
}
/**
* @param array $fields
* @return ActiveRecord|bool
* @throws Exception
*/
public function update(array $fields): static|bool
{
return $this->save($fields);
}
/**
* @param array $data
* @return bool
* @throws Exception
*/
public static function inserts(array $data): bool
{
/** @var static $class */
$class = Snowflake::createObject(['class' => static::class]);
if (empty($data)) {
return $class->addError('Insert data empty.', 'mysql');
}
return $class::find()->batchInsert($data);
}
/**
* @param array $data
* @return bool
* @throws Exception
*/
public static function inserts(array $data): bool
{
/** @var static $class */
$class = Snowflake::createObject(['class' => static::class]);
if (empty($data)) {
return $class->addError('Insert data empty.', 'mysql');
}
return $class::find()->batchInsert($data);
}
/**
* @return bool
* @throws Exception
*/
public function delete(): bool
{
$conditions = $this->_oldAttributes;
if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql');
}
$primary = $this->getPrimary();
/**
* @return bool
* @throws Exception
*/
public function delete(): bool
{
$conditions = $this->_oldAttributes;
if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql');
}
$primary = $this->getPrimary();
if (!empty($primary)) {
$conditions = [$primary => $this->getAttribute($primary)];
}
return static::deleteByCondition($conditions);
}
if (!empty($primary)) {
$conditions = [$primary => $this->getAttribute($primary)];
}
return static::deleteByCondition($conditions);
}
/**
* @param $condition
* @param array $attributes
*
* @return bool
* @throws Exception
*/
public static function updateAll(mixed $condition, $attributes = []): bool
{
$condition = static::find()->where($condition);
return $condition->batchUpdate($attributes);
}
/**
* @param $condition
* @param array $attributes
*
* @return bool
* @throws Exception
*/
public static function updateAll(mixed $condition, $attributes = []): bool
{
$condition = static::find()->where($condition);
return $condition->batchUpdate($attributes);
}
/**
* @param $condition
* @param array $attributes
*
* @return array|Collection
* @throws Exception
*/
public static function findAll($condition, $attributes = []): array|Collection
{
$query = static::find()->where($condition);
if (!empty($attributes)) {
$query->bindParams($attributes);
}
return $query->all();
}
/**
* @param $condition
* @param array $attributes
*
* @return array|Collection
* @throws Exception
*/
public static function findAll($condition, $attributes = []): array|Collection
{
$query = static::find()->where($condition);
if (!empty($attributes)) {
$query->bindParams($attributes);
}
return $query->all();
}
/**
* @param $method
* @return mixed
* @throws Exception
*/
private function resolveObject($method): mixed
{
$resolve = $this->{$this->getRelate($method)}();
if ($resolve instanceof HasBase) {
$resolve = $resolve->get();
}
if ($resolve instanceof Collection) {
return $resolve->toArray();
} else if ($resolve instanceof ActiveRecord) {
return $resolve->toArray();
} else if (is_object($resolve)) {
return get_object_vars($resolve);
} else {
return $resolve;
}
}
/**
* @param $method
* @return mixed
* @throws Exception
*/
private function resolveObject($method): mixed
{
$resolve = $this->{$this->getRelate($method)}();
if ($resolve instanceof HasBase) {
$resolve = $resolve->get();
}
if ($resolve instanceof Collection) {
return $resolve->toArray();
} else if ($resolve instanceof ActiveRecord) {
return $resolve->toArray();
} else if (is_object($resolve)) {
return get_object_vars($resolve);
} else {
return $resolve;
}
}
/**
* @return array
* @throws Exception
*/
public function toArray(): array
{
$data = $this->_attributes;
/**
* @return array
* @throws Exception
*/
public function toArray(): array
{
$data = $this->_attributes;
$lists = Snowflake::getAnnotation()->getModelMethods(get_called_class());
foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null);
}
$data = array_merge($data, $this->runRelate());
return $data;
}
$lists = Snowflake::getAnnotation()->getModelMethods(static::class);
foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null);
}
$data = array_merge($data, $this->runRelate());
return $data;
}
/**
* @return array
* @throws Exception
*/
private function runRelate(): array
{
$relates = [];
if (empty($with = $this->getWith())) {
return $relates;
}
foreach ($with as $val) {
$relates[$val] = $this->resolveObject($val);
}
return $relates;
}
/**
* @return array
* @throws Exception
*/
private function runRelate(): array
{
$relates = [];
if (empty($with = $this->getWith())) {
return $relates;
}
foreach ($with as $val) {
$relates[$val] = $this->resolveObject($val);
}
return $relates;
}
/**
* @param string $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasOne(string $modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
/**
* @param string $modelName
* @param $foreignKey
* @param $localKey
* @return HasOne|ActiveQuery
* @throws Exception
*/
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
$relation = $this->getRelation();
return new HasOne($modelName, $foreignKey, $value, $relation);
}
return new HasOne($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasCount($modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasCount($modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
$relation = $this->getRelation();
return new HasCount($modelName, $foreignKey, $value, $relation);
}
return new HasCount($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasMany($modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasMany($modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
$relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation);
}
return new HasMany($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasIn($modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery
* @throws Exception
*/
public function hasIn($modelName, $foreignKey, $localKey): mixed
{
if (!$this->has($localKey)) {
throw new Exception("Need join table primary key.");
}
$value = $this->getAttribute($localKey);
$value = $this->getAttribute($localKey);
$relation = $this->getRelation();
$relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation);
}
return new HasMany($modelName, $foreignKey, $value, $relation);
}
/**
* @return bool
* @throws Exception
*/
public function afterDelete(): bool
{
if (!$this->hasPrimary()) {
return TRUE;
}
$value = $this->getPrimaryValue();
if (empty($value)) {
return TRUE;
}
return TRUE;
}
/**
* @return bool
* @throws Exception
*/
public function afterDelete(): bool
{
if (!$this->hasPrimary()) {
return TRUE;
}
$value = $this->getPrimaryValue();
if (empty($value)) {
return TRUE;
}
return TRUE;
}
/**
* @return bool
* @throws Exception
*/
public function beforeDelete(): bool
{
if (!$this->hasPrimary()) {
return TRUE;
}
$value = $this->getPrimaryValue();
if (empty($value)) {
return TRUE;
}
return TRUE;
}
/**
* @return bool
* @throws Exception
*/
public function beforeDelete(): bool
{
if (!$this->hasPrimary()) {
return TRUE;
}
$value = $this->getPrimaryValue();
if (empty($value)) {
return TRUE;
}
return TRUE;
}
}
+9 -8
View File
@@ -350,7 +350,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function find(): ActiveQuery
{
return Snowflake::createObject(ActiveQuery::class, [get_called_class()]);
return Snowflake::createObject(ActiveQuery::class, [static::class]);
}
/**
@@ -725,14 +725,15 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
}
/**
* @param $name
* @return mixed
*/
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function getRelate($name): mixed
{
if (empty($this->_relate[$name])) {
$this->_relate = Snowflake::getAnnotation()->getRelateMethods(get_called_class());
$this->_relate = Snowflake::getAnnotation()->getRelateMethods(static::class);
}
return $this->_relate[$name] ?? null;
}
@@ -827,7 +828,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
parent::__set($name, $value);
return;
}
$method = annotation()->getSetMethodName(get_called_class(), $name);
$method = annotation()->getSetMethodName(static::class, $name);
if (!empty($method)) {
$value = $this->{$method}($value);
}
@@ -845,7 +846,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
$value = $this->_attributes[$name] ?? null;
$loader = Snowflake::app()->getAnnotation();
if (!empty($method = $loader->getGetMethodName(get_called_class(), $name))) {
if (!empty($method = $loader->getGetMethodName(static::class, $name))) {
return $this->{$method}(...[$value]);
}
if (array_key_exists($name, $this->_attributes)) {
+1 -1
View File
@@ -46,7 +46,7 @@ abstract class HttpService extends Component
if (property_exists($this, $name)) {
return $this->$name;
}
$message = sprintf('method %s::%s not exists.', get_called_class(), $name);
$message = sprintf('method %s::%s not exists.', static::class, $name);
throw new Exception($message);
}
+2 -2
View File
@@ -65,7 +65,7 @@ class BaseObject implements Configure
*/
#[Pure] public static function className(): string
{
return get_called_class();
return static::class;
}
/**
@@ -80,7 +80,7 @@ class BaseObject implements Configure
if (method_exists($this, $method)) {
$this->{$method}($value);
} else {
$this->error('set ' . $name . ' not exists ' . get_called_class());
$this->error('set ' . $name . ' not exists ' . static::class);
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
}
}
+1 -1
View File
@@ -60,7 +60,7 @@ abstract class Process extends \Swoole\Process implements SProcess
*/
#[Pure] private function getPrefix(): string
{
return get_called_class();
return static::class;
}
+1 -1
View File
@@ -128,7 +128,7 @@ abstract class BaseValidator
} else if (property_exists($this, $name)) {
$this->$name = $value;
} else {
throw new Exception('unknown property ' . $name . ' in class ' . get_called_class());
throw new Exception('unknown property ' . $name . ' in class ' . static::class);
}
}
}