Compare commits

...

3 Commits

Author SHA1 Message Date
as2252258 e5453807f2 eee 2024-09-05 15:42:24 +08:00
as2252258 5a6f6da70a eee 2024-09-02 12:12:39 +08:00
as2252258 beb48f41d2 eee 2024-08-27 11:19:22 +08:00
3 changed files with 51 additions and 7 deletions
+43 -1
View File
@@ -73,6 +73,48 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
} }
/**
* @param string $column
* @param int|float $amount
* @return bool
*/
public function increment(string $column, int|float $amount = 1): bool
{
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec();
}
/**
* @param array $attributes
* @return bool
*/
public function increments(array $attributes): bool
{
return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec();
}
/**
* @param string $column
* @param int|float $amount
* @return bool
*/
public function decrement(string $column, int|float $amount = 1): bool
{
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec();
}
/**
* @param array $attributes
* @return bool
*/
public function decrements(array $attributes): bool
{
return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec();
}
/** /**
* @throws * @throws
*/ */
@@ -95,7 +137,7 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
return; return;
} }
if (Context::inCoroutine()) { if (Context::inCoroutine()) {
Coroutine::create(fn() => $closure($data)); Coroutine::create(fn () => $closure($data));
} else { } else {
call_user_func($closure, $data); call_user_func($closure, $data);
} }
+6 -4
View File
@@ -1,4 +1,5 @@
<?php /** @noinspection ALL */ <?php
/** @noinspection ALL */
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: whwyy * User: whwyy
@@ -222,8 +223,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/ */
public function getPrimaryValue(): ?int public function getPrimaryValue(): ?int
{ {
if ($this->hasPrimary()) { if ($this->hasPrimary() && isset($this->_oldAttributes[$this->getPrimary()])) {
return (int)$this->_oldAttributes[$this->getPrimary()] ?? null; return (int)$this->_oldAttributes[$this->getPrimary()];
} else { } else {
return null; return null;
} }
@@ -234,8 +235,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @return Model|null * @return Model|null
* @throws * @throws
*/ */
public static function findOne(int|string|array $param): ?static public static function findOne(int|string|array|null $param): ?static
{ {
if (empty($param)) return null;
$model = static::instance(); $model = static::instance();
$query = new ActiveQuery($model); $query = new ActiveQuery($model);
$query->from($model->getTable())->alias('t1'); $query->from($model->getTable())->alias('t1');
+2 -2
View File
@@ -18,10 +18,10 @@ interface ModelInterface
{ {
/** /**
* @param array|string|int $param * @param array|string|int|null $param
* @return ModelInterface|null * @return ModelInterface|null
*/ */
public static function findOne(array|string|int $param): ?static; public static function findOne(array|string|int|null $param): ?static;
/** /**