This commit is contained in:
2023-04-10 11:36:33 +08:00
parent c77b2ef3eb
commit aaa3fba8ba
4 changed files with 24 additions and 12 deletions
+3 -2
View File
@@ -12,6 +12,7 @@ use Kiri\Annotation\AbstractAttribute;
/** /**
* Class Get * Class Get
* @package Annotation\Model * @package Annotation\Model
* @deprecated
*/ */
#[Attribute(Attribute::TARGET_METHOD)] class Get extends AbstractAttribute #[Attribute(Attribute::TARGET_METHOD)] class Get extends AbstractAttribute
{ {
@@ -33,8 +34,8 @@ use Kiri\Annotation\AbstractAttribute;
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
$keys = \Kiri::getDi()->get(Getter::class); // $keys = \Kiri::getDi()->get(Getter::class);
$keys->write($this->name, $class, $method); // $keys->write($this->name, $class, $method);
return true; return true;
} }
+6 -2
View File
@@ -7,6 +7,10 @@ namespace Database\Annotation;
use Database\Base\Setter; use Database\Base\Setter;
use Kiri\Annotation\AbstractAttribute; use Kiri\Annotation\AbstractAttribute;
/**
* @deprecated
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute #[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute
{ {
@@ -27,8 +31,8 @@ use Kiri\Annotation\AbstractAttribute;
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
$keys = \Kiri::getDi()->get(Setter::class); // $keys = \Kiri::getDi()->get(Setter::class);
$keys->write($this->name, $class, $method); // $keys->write($this->name, $class, $method);
return true; return true;
} }
+6
View File
@@ -75,6 +75,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
protected bool $isNewExample = TRUE; protected bool $isNewExample = TRUE;
/**
* @var array
*/
protected array $overwriteFields = [];
/** /**
* @var array * @var array
*/ */
+9 -8
View File
@@ -265,9 +265,9 @@ class Model extends Base\Model
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
$keys = Kiri::getDi()->get(Getter::class)->getAll(static::class); foreach ($this->overwriteFields as $key => $datum) {
foreach ($keys as $key => $datum) { $method = 'get' . ucfirst($key) . 'Attribute';
$data[$key] = $this->{$datum}($data[$key]); $data[$key] = $this->{$method}($datum);
} }
return $this->withRelates($data); return $this->withRelates($data);
} }
@@ -284,8 +284,8 @@ class Model extends Base\Model
} }
return $relates; return $relates;
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param $foreignKey
@@ -293,13 +293,14 @@ class Model extends Base\Model
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey):string { private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string
{
if (($value = $this->{$localKey}) === null) { if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$relation = $this->getRelation(); $relation = $this->getRelation();
$primaryKey = $modelName . $foreignKey . $value; $primaryKey = $modelName . $foreignKey . $value;
if (!$relation->hasIdentification($primaryKey)) { if (!$relation->hasIdentification($primaryKey)) {
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value])); $relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));