This commit is contained in:
2021-02-26 10:37:12 +08:00
parent 41f5c60fb5
commit 73466e415e
3 changed files with 51 additions and 29 deletions
+14
View File
@@ -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 * @param string $class
* @return ReflectionClass|null * @return ReflectionClass|null
+15 -17
View File
@@ -12,10 +12,10 @@ namespace Database\Base;
use Annotation\Event; use Annotation\Event;
use Annotation\Inject; use Annotation\Inject;
use Annotation\Model\Get;
use ArrayAccess; use ArrayAccess;
use Database\SqlBuilder; use Database\SqlBuilder;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use JetBrains\PhpStorm\Pure;
use ReflectionException; use ReflectionException;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Database\ActiveQuery; use Database\ActiveQuery;
@@ -68,6 +68,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
private array $_annotations = []; private array $_annotations = [];
private array $_fields = [];
/** /**
* @var bool * @var bool
*/ */
@@ -109,6 +112,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} else { } else {
$this->_relation = Context::getContext(Relation::class); $this->_relation = Context::getContext(Relation::class);
} }
$this->_fields = static::getColumns()->format();
$this->createAnnotation(); $this->createAnnotation();
} }
@@ -120,17 +126,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{ {
$annotation = Snowflake::app()->getAttributes(); $annotation = Snowflake::app()->getAttributes();
$name = static::class; $this->_annotations = $annotation->getMethods(get_called_class());
$this->_annotations = $annotation->getMethods($name); $annotation->setProperty($this);
$lists = $annotation->getProperty($name);
if (empty($lists)) {
return;
}
foreach ($lists as $name => $list) {
$this->{$name} = $list;
}
} }
@@ -576,11 +574,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
$_tmp = []; $_tmp = [];
$condition = []; $condition = [];
$columns = static::getColumns(); $columns = static::getColumns();
$format = $columns->getAllField();
foreach ($this->_attributes as $key => $val) { foreach ($this->_attributes as $key => $val) {
$oldValue = $this->_oldAttributes[$key] ?? null; $oldValue = $this->_oldAttributes[$key] ?? null;
if ($val !== $oldValue) { if ($val !== $oldValue) {
$_tmp[$key] = $columns->fieldFormat($key, $val); $_tmp[$key] = $columns->encode($val, $columns->clean($format[$key]));
} else { } else {
$condition[$key] = $val; $condition[$key] = $val;
} }
@@ -647,11 +647,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function has($attribute): bool #[Pure] public function has($attribute): bool
{ {
$format = static::getColumns()->format(); return array_key_exists($attribute, $this->_fields);
return array_key_exists($attribute, $format);
} }
/**ƒ /**ƒ
+21 -11
View File
@@ -12,6 +12,7 @@ namespace Database\Mysql;
use Database\SqlBuilder; use Database\SqlBuilder;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Database\Connection; use Database\Connection;
use Exception; use Exception;
@@ -139,10 +140,10 @@ class Columns extends Component
/** /**
* @param $val * @param $val
* @param null $format * @param null $format
* @return mixed * @return float|bool|int|string
* @throws Exception * @throws Exception
*/ */
public function encode($val, $format = null): mixed public function encode($val, $format = null): float|bool|int|string
{ {
if (empty($format)) { if (empty($format)) {
return $val; return $val;
@@ -163,7 +164,7 @@ class Columns extends Component
* @param $format * @param $format
* @return bool * @return bool
*/ */
public function isInt($format): bool #[Pure] public function isInt($format): bool
{ {
return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']); return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']);
} }
@@ -172,7 +173,7 @@ class Columns extends Component
* @param $format * @param $format
* @return bool * @return bool
*/ */
public function isFloat($format): bool #[Pure] public function isFloat($format): bool
{ {
return in_array($format, ['float', 'double', 'decimal']); return in_array($format, ['float', 'double', 'decimal']);
} }
@@ -181,7 +182,7 @@ class Columns extends Component
* @param $format * @param $format
* @return bool * @return bool
*/ */
public function isJson($format): bool #[Pure] public function isJson($format): bool
{ {
return in_array($format, ['json']); return in_array($format, ['json']);
} }
@@ -190,7 +191,7 @@ class Columns extends Component
* @param $format * @param $format
* @return bool * @return bool
*/ */
public function isString($format): bool #[Pure] public function isString($format): bool
{ {
return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']); return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']);
} }
@@ -232,7 +233,7 @@ class Columns extends Component
* *
* @throws Exception * @throws Exception
*/ */
public function getFirstPrimary(): array|string|null #[Pure] public function getFirstPrimary(): array|string|null
{ {
if (isset($this->_auto_increment[$this->table])) { if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table]; return $this->_auto_increment[$this->table];
@@ -334,7 +335,7 @@ class Columns extends Component
* @param $type * @param $type
* @return string * @return string
*/ */
private function clean($type): string public function clean($type): string
{ {
if (!str_contains($type, ')')) { if (!str_contains($type, ')')) {
return $type; return $type;
@@ -353,14 +354,23 @@ class Columns extends Component
*/ */
public function get_fields($field = null): array|string|null public function get_fields($field = null): array|string|null
{ {
$fields = $this->columns('Type', 'Field'); $fields = $this->getAllField();
if (empty($field)) { if (empty($field)) {
return $fields; return $fields;
} }
if (!isset($fields[$field])) { if (isset($fields[$field])) {
return strtolower($fields[$field]);
}
return null; return null;
} }
return strtolower($fields[$field]);
/**
* @return array
* @throws Exception
*/
public function getAllField(): array
{
return $this->columns('Type', 'Field');
} }
} }