diff --git a/Database/ActiveQuery.php b/Database/ActiveQuery.php index 29592e38..f1eaf24f 100644 --- a/Database/ActiveQuery.php +++ b/Database/ActiveQuery.php @@ -410,6 +410,6 @@ class ActiveQuery extends Component */ public function getPrimary() { - return $this->modelClass::getPrimary(); + return $this->modelClass->getPrimary(); } } diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index d20d5182..d4de2324 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -135,7 +135,7 @@ class ActiveRecord extends BaseActiveRecord private function mathematics($action, $columns, $condition = []) { if (empty($condition)) { - $condition = [static::getPrimary() => $this->{static::getPrimary()}]; + $condition = [$this->getPrimary() => $this->getPrimaryValue()]; } return static::getDb()->createCommand() ->mathematics(self::getTable(), [$action => $columns], $condition) @@ -223,7 +223,7 @@ class ActiveRecord extends BaseActiveRecord if (empty($conditions)) { return $this->addError("Delete condition do not empty.", 'mysql'); } - $primary = static::getPrimary(); + $primary = $this->getPrimary(); if (!empty($primary)) { $sul = static::deleteAll([$primary => $this->getAttribute($primary)]); @@ -401,10 +401,10 @@ class ActiveRecord extends BaseActiveRecord */ public function afterDelete() { - if (!static::hasPrimary()) { + if (!$this->hasPrimary()) { return TRUE; } - $value = $this->{static::getPrimary()}; + $value = $this->getPrimaryValue(); if (empty($value)) { return TRUE; } @@ -417,10 +417,10 @@ class ActiveRecord extends BaseActiveRecord */ public function beforeDelete() { - if (!static::hasPrimary()) { + if (!$this->hasPrimary()) { return TRUE; } - $value = $this->{static::getPrimary()}; + $value = $this->getPrimaryValue(); if (empty($value)) { return TRUE; } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index af095400..a3b56287 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -47,7 +47,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess protected $_relate = []; /** @var null|string */ - protected static $primary = NULL; + protected $primary = NULL; /** * @var bool @@ -130,14 +130,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess * @return bool * @throws Exception */ - public static function hasPrimary() + public function hasPrimary() { - if (static::$primary !== NULL) { + if ($this->primary !== NULL) { return true; } $primary = static::getColumns()->getPrimaryKeys(); if (!empty($primary)) { - return true; + return $this->primary = current($primary); } return false; } @@ -163,19 +163,24 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess * @return null|string * @throws Exception */ - public static function getPrimary() + public function getPrimary() { - if (!static::hasPrimary()) { + if (!$this->hasPrimary()) { return null; } - if (!empty(static::$primary)) { - return static::$primary; + return $this->primary; + } + + /** + * @return null|string + * @throws Exception + */ + public function getPrimaryValue() + { + if (!$this->hasPrimary()) { + return null; } - $primary = static::getColumns()->getPrimaryKeys(); - if (!empty($primary)) { - return is_array($primary) ? current($primary) : $primary; - } - return null; + return $this->getAttribute($this->primary); } /** @@ -186,10 +191,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess */ public static function findOne($condition, $db = NULL) { - if (empty($condition) || !is_numeric($condition)) { - return NULL; + if (is_numeric($condition)) { + $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) { + $columns = static::getColumns(); if (empty($field)) { - $field = self::getPrimary(); + $field = $columns->getFirstPrimary(); } - - $columns = static::getColumns()->get_fields(); + $columns = $columns->get_fields(); if (!isset($columns[$field])) { return null; } - $first = static::find()->max($field)->first(); if (empty($first)) { return null; @@ -288,9 +296,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess /** * @param array $param * @return $this - * @throws + * @throws Exception */ - public function setAttributes($param) + public function setAttributes(array $param) { if (empty($param) || !is_array($param)) { return $this; @@ -305,6 +313,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess return $this; } + /** + * @param $param + * @return $this + */ public function setOldAttributes($param) { if (empty($param) || !is_array($param)) { @@ -348,8 +360,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess } if ($this->hasAutoIncrement()) { $this->setAttribute($this->getAutoIncrement(), (int)$lastId); - } else if (static::hasPrimary()) { - $primary = static::getPrimary(); + } else if ($this->hasPrimary()) { + $primary = $this->getPrimary(); if (!isset($param[$primary]) || empty($param[$primary])) { $this->setAttribute($primary, (int)$lastId); } @@ -411,8 +423,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess static::getDb()->enablingTransactions(); [$attributes, $condition, $param] = $this->filtration_and_separation(); - if (($primary = static::getPrimary()) !== null) { - $condition = [$primary => $this->getPrValue()]; + if (($primary = $this->getPrimary()) !== null) { + $condition = [$primary => $this->getPrimaryValue()]; } if (!$this->getIsCreate()) { @@ -427,7 +439,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess * @return bool * @throws Exception */ - public function validator($rule) + public function validator(array $rule) { if (empty($rule)) return true; $validate = $this->resolve($rule); @@ -607,15 +619,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess return static::setDatabaseConnect('db'); } - /** - * @return mixed - * @throws Exception - */ - public function getPrValue() - { - return $this->getAttribute(static::getPrimary()); - } - /** * @return static */ diff --git a/Database/Collection.php b/Database/Collection.php index 3abf4cee..174935e3 100644 --- a/Database/Collection.php +++ b/Database/Collection.php @@ -170,19 +170,19 @@ class Collection extends AbstractCollection public function delete() { $model = $this->model; - if (!$model::hasPrimary()) { + if (!$model->hasPrimary()) { return false; } $ids = []; foreach ($this as $item) { - $ids[] = $item->{$model::getPrimary()}; + $ids[] = $item->getPrimaryValue(); } $ids = array_filter($ids); if (empty($ids)) { return false; } return $this->model::find() - ->in($model::getPrimary(), $ids) + ->in($model->getPrimary(), $ids) ->deleteAll(); } diff --git a/Database/Db.php b/Database/Db.php index 83b377ac..7dbd85c8 100644 --- a/Database/Db.php +++ b/Database/Db.php @@ -10,7 +10,6 @@ namespace Database; use Database\Traits\QueryTrait; use Exception; -use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; /** diff --git a/Database/Mysql/Columns.php b/Database/Mysql/Columns.php index c95818fe..f09b5bfb 100644 --- a/Database/Mysql/Columns.php +++ b/Database/Mysql/Columns.php @@ -189,6 +189,22 @@ class Columns extends Component 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 null $index diff --git a/Database/Pagination.php b/Database/Pagination.php index 41616297..b1183e57 100644 --- a/Database/Pagination.php +++ b/Database/Pagination.php @@ -46,7 +46,7 @@ class Pagination extends Component /** - * @param Closure $callback + * @param Closure|array $callback * @throws Exception */ public function setCallback($callback)