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
+15 -17
View File
@@ -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);
}
/**ƒ
+22 -12
View File
@@ -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');
}
}