This commit is contained in:
2020-09-11 17:58:02 +08:00
parent 2d789b3494
commit 45aaf011f9
7 changed files with 66 additions and 48 deletions
+1 -1
View File
@@ -410,6 +410,6 @@ class ActiveQuery extends Component
*/ */
public function getPrimary() public function getPrimary()
{ {
return $this->modelClass::getPrimary(); return $this->modelClass->getPrimary();
} }
} }
+6 -6
View File
@@ -135,7 +135,7 @@ class ActiveRecord extends BaseActiveRecord
private function mathematics($action, $columns, $condition = []) private function mathematics($action, $columns, $condition = [])
{ {
if (empty($condition)) { if (empty($condition)) {
$condition = [static::getPrimary() => $this->{static::getPrimary()}]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
} }
return static::getDb()->createCommand() return static::getDb()->createCommand()
->mathematics(self::getTable(), [$action => $columns], $condition) ->mathematics(self::getTable(), [$action => $columns], $condition)
@@ -223,7 +223,7 @@ class ActiveRecord extends BaseActiveRecord
if (empty($conditions)) { if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql'); return $this->addError("Delete condition do not empty.", 'mysql');
} }
$primary = static::getPrimary(); $primary = $this->getPrimary();
if (!empty($primary)) { if (!empty($primary)) {
$sul = static::deleteAll([$primary => $this->getAttribute($primary)]); $sul = static::deleteAll([$primary => $this->getAttribute($primary)]);
@@ -401,10 +401,10 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public function afterDelete() public function afterDelete()
{ {
if (!static::hasPrimary()) { if (!$this->hasPrimary()) {
return TRUE; return TRUE;
} }
$value = $this->{static::getPrimary()}; $value = $this->getPrimaryValue();
if (empty($value)) { if (empty($value)) {
return TRUE; return TRUE;
} }
@@ -417,10 +417,10 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public function beforeDelete() public function beforeDelete()
{ {
if (!static::hasPrimary()) { if (!$this->hasPrimary()) {
return TRUE; return TRUE;
} }
$value = $this->{static::getPrimary()}; $value = $this->getPrimaryValue();
if (empty($value)) { if (empty($value)) {
return TRUE; return TRUE;
} }
+39 -36
View File
@@ -47,7 +47,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
protected $_relate = []; protected $_relate = [];
/** @var null|string */ /** @var null|string */
protected static $primary = NULL; protected $primary = NULL;
/** /**
* @var bool * @var bool
@@ -130,14 +130,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function hasPrimary() public function hasPrimary()
{ {
if (static::$primary !== NULL) { if ($this->primary !== NULL) {
return true; return true;
} }
$primary = static::getColumns()->getPrimaryKeys(); $primary = static::getColumns()->getPrimaryKeys();
if (!empty($primary)) { if (!empty($primary)) {
return true; return $this->primary = current($primary);
} }
return false; return false;
} }
@@ -163,19 +163,24 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return null|string * @return null|string
* @throws Exception * @throws Exception
*/ */
public static function getPrimary() public function getPrimary()
{ {
if (!static::hasPrimary()) { if (!$this->hasPrimary()) {
return null; return null;
} }
if (!empty(static::$primary)) { return $this->primary;
return static::$primary; }
/**
* @return null|string
* @throws Exception
*/
public function getPrimaryValue()
{
if (!$this->hasPrimary()) {
return null;
} }
$primary = static::getColumns()->getPrimaryKeys(); return $this->getAttribute($this->primary);
if (!empty($primary)) {
return is_array($primary) ? current($primary) : $primary;
}
return null;
} }
/** /**
@@ -186,10 +191,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
*/ */
public static function findOne($condition, $db = NULL) public static function findOne($condition, $db = NULL)
{ {
if (empty($condition) || !is_numeric($condition)) { if (is_numeric($condition)) {
return NULL; $primary = static::getColumns()->getPrimaryKeys();
if (empty($primary)) {
throw new Exception('Primary key cannot be empty.');
}
$condition = [current($primary) => $condition];
} }
return static::find()->where([static::getPrimary() => $condition])->first(); return static::find()->where($condition)->first();
} }
/** /**
@@ -200,15 +209,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
*/ */
public static function max($field = null) public static function max($field = null)
{ {
$columns = static::getColumns();
if (empty($field)) { if (empty($field)) {
$field = self::getPrimary(); $field = $columns->getFirstPrimary();
} }
$columns = $columns->get_fields();
$columns = static::getColumns()->get_fields();
if (!isset($columns[$field])) { if (!isset($columns[$field])) {
return null; return null;
} }
$first = static::find()->max($field)->first(); $first = static::find()->max($field)->first();
if (empty($first)) { if (empty($first)) {
return null; return null;
@@ -288,9 +296,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @param array $param * @param array $param
* @return $this * @return $this
* @throws * @throws Exception
*/ */
public function setAttributes($param) public function setAttributes(array $param)
{ {
if (empty($param) || !is_array($param)) { if (empty($param) || !is_array($param)) {
return $this; return $this;
@@ -305,6 +313,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
return $this; return $this;
} }
/**
* @param $param
* @return $this
*/
public function setOldAttributes($param) public function setOldAttributes($param)
{ {
if (empty($param) || !is_array($param)) { if (empty($param) || !is_array($param)) {
@@ -348,8 +360,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
} }
if ($this->hasAutoIncrement()) { if ($this->hasAutoIncrement()) {
$this->setAttribute($this->getAutoIncrement(), (int)$lastId); $this->setAttribute($this->getAutoIncrement(), (int)$lastId);
} else if (static::hasPrimary()) { } else if ($this->hasPrimary()) {
$primary = static::getPrimary(); $primary = $this->getPrimary();
if (!isset($param[$primary]) || empty($param[$primary])) { if (!isset($param[$primary]) || empty($param[$primary])) {
$this->setAttribute($primary, (int)$lastId); $this->setAttribute($primary, (int)$lastId);
} }
@@ -411,8 +423,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
static::getDb()->enablingTransactions(); static::getDb()->enablingTransactions();
[$attributes, $condition, $param] = $this->filtration_and_separation(); [$attributes, $condition, $param] = $this->filtration_and_separation();
if (($primary = static::getPrimary()) !== null) { if (($primary = $this->getPrimary()) !== null) {
$condition = [$primary => $this->getPrValue()]; $condition = [$primary => $this->getPrimaryValue()];
} }
if (!$this->getIsCreate()) { if (!$this->getIsCreate()) {
@@ -427,7 +439,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function validator($rule) public function validator(array $rule)
{ {
if (empty($rule)) return true; if (empty($rule)) return true;
$validate = $this->resolve($rule); $validate = $this->resolve($rule);
@@ -607,15 +619,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
return static::setDatabaseConnect('db'); return static::setDatabaseConnect('db');
} }
/**
* @return mixed
* @throws Exception
*/
public function getPrValue()
{
return $this->getAttribute(static::getPrimary());
}
/** /**
* @return static * @return static
*/ */
+3 -3
View File
@@ -170,19 +170,19 @@ class Collection extends AbstractCollection
public function delete() public function delete()
{ {
$model = $this->model; $model = $this->model;
if (!$model::hasPrimary()) { if (!$model->hasPrimary()) {
return false; return false;
} }
$ids = []; $ids = [];
foreach ($this as $item) { foreach ($this as $item) {
$ids[] = $item->{$model::getPrimary()}; $ids[] = $item->getPrimaryValue();
} }
$ids = array_filter($ids); $ids = array_filter($ids);
if (empty($ids)) { if (empty($ids)) {
return false; return false;
} }
return $this->model::find() return $this->model::find()
->in($model::getPrimary(), $ids) ->in($model->getPrimary(), $ids)
->deleteAll(); ->deleteAll();
} }
-1
View File
@@ -10,7 +10,6 @@ namespace Database;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
use Exception; use Exception;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
+16
View File
@@ -189,6 +189,22 @@ class Columns extends Component
return $this->_primary[$this->table] ?? null; return $this->_primary[$this->table] ?? null;
} }
/**
* @return array|null|string
*
* @throws Exception
*/
public function getFirstPrimary()
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
}
if (isset($this->_primary[$this->table])) {
return current($this->_primary[$this->table]);
}
return null;
}
/** /**
* @param $name * @param $name
* @param null $index * @param null $index
+1 -1
View File
@@ -46,7 +46,7 @@ class Pagination extends Component
/** /**
* @param Closure $callback * @param Closure|array $callback
* @throws Exception * @throws Exception
*/ */
public function setCallback($callback) public function setCallback($callback)