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
* @package Annotation\Model
* @deprecated
*/
#[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
{
$keys = \Kiri::getDi()->get(Getter::class);
$keys->write($this->name, $class, $method);
// $keys = \Kiri::getDi()->get(Getter::class);
// $keys->write($this->name, $class, $method);
return true;
}
+6 -2
View File
@@ -7,6 +7,10 @@ namespace Database\Annotation;
use Database\Base\Setter;
use Kiri\Annotation\AbstractAttribute;
/**
* @deprecated
*/
#[\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
{
$keys = \Kiri::getDi()->get(Setter::class);
$keys->write($this->name, $class, $method);
// $keys = \Kiri::getDi()->get(Setter::class);
// $keys->write($this->name, $class, $method);
return true;
}
+6
View File
@@ -75,6 +75,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
protected bool $isNewExample = TRUE;
/**
* @var array
*/
protected array $overwriteFields = [];
/**
* @var array
*/
+9 -8
View File
@@ -265,9 +265,9 @@ class Model extends Base\Model
public function toArray(): array
{
$data = $this->_attributes;
$keys = Kiri::getDi()->get(Getter::class)->getAll(static::class);
foreach ($keys as $key => $datum) {
$data[$key] = $this->{$datum}($data[$key]);
foreach ($this->overwriteFields as $key => $datum) {
$method = 'get' . ucfirst($key) . 'Attribute';
$data[$key] = $this->{$method}($datum);
}
return $this->withRelates($data);
}
@@ -284,8 +284,8 @@ class Model extends Base\Model
}
return $relates;
}
/**
* @param ModelInterface|string $modelName
* @param $foreignKey
@@ -293,13 +293,14 @@ class Model extends Base\Model
* @return string
* @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) {
throw new Exception("Need join table primary key.");
}
$relation = $this->getRelation();
$primaryKey = $modelName . $foreignKey . $value;
if (!$relation->hasIdentification($primaryKey)) {
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));