diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index a79e1e24..87b2ed3f 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -201,6 +201,20 @@ class Annotation extends Component } + /** + * @param $class + * @return void + */ + public function setProperty($class): void + { + $lists = $this->getProperty($class); + if (empty($lists)) { + return; + } + Snowflake::configure($class, $lists); + } + + /** * @param string $class * @return ReflectionClass|null diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 201c2882..64c49c00 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -12,10 +12,10 @@ namespace Database\Base; use Annotation\Event; use Annotation\Inject; -use Annotation\Model\Get; use ArrayAccess; use Database\SqlBuilder; use HttpServer\Http\Context; +use JetBrains\PhpStorm\Pure; use ReflectionException; use Snowflake\Abstracts\Component; use Database\ActiveQuery; @@ -68,6 +68,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess private array $_annotations = []; + + private array $_fields = []; + /** * @var bool */ @@ -109,6 +112,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess } else { $this->_relation = Context::getContext(Relation::class); } + + $this->_fields = static::getColumns()->format(); + $this->createAnnotation(); } @@ -120,17 +126,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess { $annotation = Snowflake::app()->getAttributes(); - $name = static::class; + $this->_annotations = $annotation->getMethods(get_called_class()); - $this->_annotations = $annotation->getMethods($name); - - $lists = $annotation->getProperty($name); - if (empty($lists)) { - return; - } - foreach ($lists as $name => $list) { - $this->{$name} = $list; - } + $annotation->setProperty($this); } @@ -576,11 +574,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess $_tmp = []; $condition = []; $columns = static::getColumns(); + + $format = $columns->getAllField(); + foreach ($this->_attributes as $key => $val) { $oldValue = $this->_oldAttributes[$key] ?? null; - if ($val !== $oldValue) { - $_tmp[$key] = $columns->fieldFormat($key, $val); + $_tmp[$key] = $columns->encode($val, $columns->clean($format[$key])); } else { $condition[$key] = $val; } @@ -647,11 +647,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess * @return bool * @throws Exception */ - public function has($attribute): bool + #[Pure] public function has($attribute): bool { - $format = static::getColumns()->format(); - - return array_key_exists($attribute, $format); + return array_key_exists($attribute, $this->_fields); } /**ƒ diff --git a/Database/Mysql/Columns.php b/Database/Mysql/Columns.php index 461e2de6..73b9e9bd 100644 --- a/Database/Mysql/Columns.php +++ b/Database/Mysql/Columns.php @@ -12,6 +12,7 @@ namespace Database\Mysql; use Database\SqlBuilder; +use JetBrains\PhpStorm\Pure; use Snowflake\Abstracts\Component; use Database\Connection; use Exception; @@ -139,10 +140,10 @@ class Columns extends Component /** * @param $val * @param null $format - * @return mixed + * @return float|bool|int|string * @throws Exception */ - public function encode($val, $format = null): mixed + public function encode($val, $format = null): float|bool|int|string { if (empty($format)) { return $val; @@ -163,7 +164,7 @@ class Columns extends Component * @param $format * @return bool */ - public function isInt($format): bool + #[Pure] public function isInt($format): bool { return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']); } @@ -172,7 +173,7 @@ class Columns extends Component * @param $format * @return bool */ - public function isFloat($format): bool + #[Pure] public function isFloat($format): bool { return in_array($format, ['float', 'double', 'decimal']); } @@ -181,7 +182,7 @@ class Columns extends Component * @param $format * @return bool */ - public function isJson($format): bool + #[Pure] public function isJson($format): bool { return in_array($format, ['json']); } @@ -190,7 +191,7 @@ class Columns extends Component * @param $format * @return bool */ - public function isString($format): bool + #[Pure] public function isString($format): bool { return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']); } @@ -232,7 +233,7 @@ class Columns extends Component * * @throws Exception */ - public function getFirstPrimary(): array|string|null + #[Pure] public function getFirstPrimary(): array|string|null { if (isset($this->_auto_increment[$this->table])) { return $this->_auto_increment[$this->table]; @@ -334,7 +335,7 @@ class Columns extends Component * @param $type * @return string */ - private function clean($type): string + public function clean($type): string { if (!str_contains($type, ')')) { return $type; @@ -353,14 +354,23 @@ class Columns extends Component */ public function get_fields($field = null): array|string|null { - $fields = $this->columns('Type', 'Field'); + $fields = $this->getAllField(); if (empty($field)) { return $fields; } - if (!isset($fields[$field])) { - return null; + if (isset($fields[$field])) { + return strtolower($fields[$field]); } - return strtolower($fields[$field]); + return null; + } + + /** + * @return array + * @throws Exception + */ + public function getAllField(): array + { + return $this->columns('Type', 'Field'); } }