2022-01-09 03:49:51 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/3/30 0030
|
|
|
|
|
* Time: 14:39
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
|
|
|
2023-03-30 20:39:00 +08:00
|
|
|
use Database\Base\Getter;
|
2022-01-09 03:49:51 +08:00
|
|
|
use Exception;
|
2022-01-12 14:10:32 +08:00
|
|
|
use Kiri;
|
2022-01-18 18:06:24 +08:00
|
|
|
use Kiri\Exception\NotFindClassException;
|
2022-02-23 18:02:28 +08:00
|
|
|
use Kiri\Error\StdoutLoggerInterface;
|
2022-01-09 03:49:51 +08:00
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
|
|
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
|
|
|
|
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Orm
|
|
|
|
|
* @package Database
|
|
|
|
|
*
|
|
|
|
|
* @property $attributes
|
|
|
|
|
* @property-read $oldAttributes
|
|
|
|
|
*/
|
|
|
|
|
class Model extends Base\Model
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $column
|
|
|
|
|
* @param int $value
|
|
|
|
|
* @return ModelInterface|false
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function increment(string $column, int $value): bool|ModelInterface
|
|
|
|
|
{
|
|
|
|
|
if (!$this->mathematics([$column => $value], '+')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->{$column} += $value;
|
|
|
|
|
return $this->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $column
|
|
|
|
|
* @param int $value
|
|
|
|
|
* @return ModelInterface|false
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function decrement(string $column, int $value): bool|ModelInterface
|
|
|
|
|
{
|
|
|
|
|
if (!$this->mathematics([$column => $value], '-')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->{$column} -= $value;
|
|
|
|
|
return $this->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $columns
|
|
|
|
|
* @return ModelInterface|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 ModelInterface|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|ModelInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function findOrCreate(array $condition, array $attributes): bool|static
|
|
|
|
|
{
|
2022-02-23 18:02:28 +08:00
|
|
|
$logger = Kiri::getDi()->get(StdoutLoggerInterface::class);
|
2022-01-09 03:49:51 +08:00
|
|
|
if (empty($attributes)) {
|
|
|
|
|
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
|
|
|
|
}
|
2023-04-01 17:11:47 +08:00
|
|
|
|
2022-01-19 14:32:23 +08:00
|
|
|
/** @var static $select */
|
|
|
|
|
$select = static::query()->where($condition)->first();
|
2022-06-22 16:29:42 +08:00
|
|
|
if (!empty($select)) {
|
|
|
|
|
return $select;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$select = new static();
|
|
|
|
|
$select->setAttributes($condition);
|
|
|
|
|
$select->setAttributes($attributes);
|
|
|
|
|
if (!$select->save()) {
|
|
|
|
|
throw new Exception($select->getLastError());
|
2022-01-18 18:06:24 +08:00
|
|
|
}
|
2022-01-19 14:32:23 +08:00
|
|
|
return $select;
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $condition
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
* @return bool|static
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
|
|
|
|
|
{
|
2022-02-23 18:02:28 +08:00
|
|
|
$logger = Kiri::getDi()->get(StdoutLoggerInterface::class);
|
2022-01-09 03:49:51 +08:00
|
|
|
if (empty($attributes)) {
|
|
|
|
|
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
|
|
|
|
}
|
2022-01-19 14:32:23 +08:00
|
|
|
/** @var static $select */
|
|
|
|
|
$select = static::query()->where($condition)->first();
|
|
|
|
|
if (empty($select)) {
|
2022-02-28 14:40:13 +08:00
|
|
|
$select = new static();
|
2022-06-22 16:29:42 +08:00
|
|
|
$select->setAttributes($condition);
|
2022-01-19 14:32:23 +08:00
|
|
|
}
|
2022-06-22 16:29:42 +08:00
|
|
|
$select->setAttributes($attributes);
|
2022-01-19 14:32:23 +08:00
|
|
|
if (!$select->save()) {
|
|
|
|
|
throw new Exception($select->getLastError());
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2022-01-19 14:32:23 +08:00
|
|
|
return $select;
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $columns
|
2022-06-22 16:29:42 +08:00
|
|
|
* @param $action
|
2022-01-09 03:49:51 +08:00
|
|
|
* @return array|bool|int|string|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-06-22 16:29:42 +08:00
|
|
|
private function mathematics($columns, $action): int|bool|array|string|null
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2022-06-22 16:29:42 +08:00
|
|
|
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
2022-01-09 03:49:51 +08:00
|
|
|
|
|
|
|
|
$activeQuery = static::query()->where($condition);
|
|
|
|
|
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
|
|
|
|
|
if (is_bool($create)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return $this->getConnection()->createCommand($create[0], $create[1])->exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $fields
|
|
|
|
|
* @return ModelInterface|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
|
|
|
|
|
{
|
|
|
|
|
if (empty($data)) {
|
2022-06-22 16:29:42 +08:00
|
|
|
return error('Insert data empty.', 'mysql');
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2022-09-07 14:36:32 +08:00
|
|
|
return static::query()->insert($data);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function delete(): bool
|
|
|
|
|
{
|
|
|
|
|
$primary = $this->getPrimary();
|
|
|
|
|
if (empty($primary) || !$this->hasPrimaryValue()) {
|
2022-02-23 16:32:08 +08:00
|
|
|
return $this->logger->addError("Only primary key operations are supported.", 'mysql');
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2022-06-22 16:29:42 +08:00
|
|
|
if (!$this->beforeDelete()) {
|
|
|
|
|
return false;
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2022-06-22 16:29:42 +08:00
|
|
|
$result = static::deleteByCondition([$primary => $this->getPrimaryValue()]);
|
|
|
|
|
|
|
|
|
|
$this->afterDelete($result);
|
|
|
|
|
|
|
|
|
|
return $result;
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $condition
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function updateAll(mixed $condition, array $attributes = []): bool
|
|
|
|
|
{
|
|
|
|
|
$condition = static::query()->where($condition);
|
2022-09-07 14:36:32 +08:00
|
|
|
return $condition->update($attributes);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $condition
|
|
|
|
|
* @return array|Collection
|
|
|
|
|
*/
|
|
|
|
|
public static function get($condition): Collection|array
|
|
|
|
|
{
|
|
|
|
|
return static::query()->where($condition)->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $condition
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*
|
|
|
|
|
* @return array|Collection
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function findAll($condition, array $attributes = []): array|Collection
|
|
|
|
|
{
|
|
|
|
|
$query = static::query()->where($condition);
|
|
|
|
|
if (!empty($attributes)) {
|
|
|
|
|
$query->bindParams($attributes);
|
|
|
|
|
}
|
|
|
|
|
return $query->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function toArray(): array
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_attributes;
|
2023-04-01 01:27:57 +08:00
|
|
|
$keys = Kiri::getDi()->get(Getter::class)->getAll(static::class);
|
|
|
|
|
foreach ($keys as $key => $datum) {
|
2023-03-30 20:39:00 +08:00
|
|
|
$data[$key] = $this->{$datum}($data[$key]);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
return $this->withRelates($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $relates
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function withRelates($relates): array
|
|
|
|
|
{
|
2023-04-01 17:11:47 +08:00
|
|
|
foreach ($this->_with as $val) {
|
2022-06-22 16:29:42 +08:00
|
|
|
$relates[$val] = $this->withRelate($val);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
return $relates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 17:11:47 +08:00
|
|
|
* @param ModelInterface|string $modelName
|
2022-01-09 03:49:51 +08:00
|
|
|
* @param $foreignKey
|
|
|
|
|
* @param $localKey
|
|
|
|
|
* @return HasOne|ActiveQuery
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 17:11:47 +08:00
|
|
|
public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
|
|
|
|
if (($value = $this->{$localKey}) === null) {
|
|
|
|
|
throw new Exception("Need join table primary key.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$relation = $this->getRelation();
|
|
|
|
|
|
2023-04-01 17:11:47 +08:00
|
|
|
$primaryKey = $modelName . $foreignKey . $value;
|
|
|
|
|
if (!$relation->hasIdentification($primaryKey)) {
|
|
|
|
|
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HasOne($primaryKey, $relation);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 17:11:47 +08:00
|
|
|
* @param ModelInterface|string $modelName
|
2022-01-09 03:49:51 +08:00
|
|
|
* @param $foreignKey
|
|
|
|
|
* @param $localKey
|
|
|
|
|
* @return ActiveQuery|HasCount
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 17:11:47 +08:00
|
|
|
public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
|
|
|
|
if (($value = $this->{$localKey}) === null) {
|
|
|
|
|
throw new Exception("Need join table primary key.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$relation = $this->getRelation();
|
|
|
|
|
|
2023-04-01 17:11:47 +08:00
|
|
|
$primaryKey = $modelName . $foreignKey . $value;
|
|
|
|
|
if (!$relation->hasIdentification($primaryKey)) {
|
|
|
|
|
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HasCount($primaryKey);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 17:11:47 +08:00
|
|
|
* @param ModelInterface|string $modelName
|
2022-01-09 03:49:51 +08:00
|
|
|
* @param $foreignKey
|
|
|
|
|
* @param $localKey
|
|
|
|
|
* @return ActiveQuery|HasMany
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 17:11:47 +08:00
|
|
|
public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
|
|
|
|
if (($value = $this->{$localKey}) === null) {
|
|
|
|
|
throw new Exception("Need join table primary key.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$relation = $this->getRelation();
|
|
|
|
|
|
2023-04-01 17:11:47 +08:00
|
|
|
$primaryKey = $modelName . $foreignKey . $value;
|
|
|
|
|
if (!$relation->hasIdentification($primaryKey)) {
|
|
|
|
|
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HasMany($primaryKey);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 17:11:47 +08:00
|
|
|
* @param ModelInterface|string $modelName
|
2022-01-09 03:49:51 +08:00
|
|
|
* @param $foreignKey
|
|
|
|
|
* @param $localKey
|
|
|
|
|
* @return ActiveQuery|HasMany
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 17:11:47 +08:00
|
|
|
public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
|
|
|
|
if (($value = $this->{$localKey}) === null) {
|
|
|
|
|
throw new Exception("Need join table primary key.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$relation = $this->getRelation();
|
|
|
|
|
|
2023-04-01 17:11:47 +08:00
|
|
|
$primaryKey = $modelName . $foreignKey . json_encode($value, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
if (!$relation->hasIdentification($primaryKey)) {
|
|
|
|
|
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HasMany($primaryKey);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $result
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function afterDelete(bool $result): void
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function beforeDelete(): bool
|
|
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|