Skip to content

Commit b8330e9

Browse files
committed
fix: processing in Model of Entity for which primary key is cast
1 parent 2ee4979 commit b8330e9

1 file changed

Lines changed: 16 additions & 31 deletions

File tree

system/Model.php

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ protected function idValue($data)
534534
public function getIdValue($data)
535535
{
536536
if (is_object($data) && isset($data->{$this->primaryKey})) {
537+
// Get the raw primary key value of the Entity.
538+
if ($data instanceof Entity) {
539+
$cast = $data->cast();
540+
541+
// Disable Entity casting, because raw primary key value is needed for database.
542+
$data->cast(false);
543+
544+
$primaryKey = $data->{$this->primaryKey};
545+
546+
// Restore Entity casting setting.
547+
$data->cast($cast);
548+
549+
return $primaryKey;
550+
}
551+
537552
return $data->{$this->primaryKey};
538553
}
539554

@@ -796,37 +811,7 @@ public function update($id = null, $data = null): bool
796811
*/
797812
protected function objectToRawArray($data, bool $onlyChanged = true, bool $recursive = false): ?array
798813
{
799-
$properties = parent::objectToRawArray($data, $onlyChanged);
800-
801-
$primaryKey = null;
802-
803-
if ($data instanceof Entity) {
804-
$cast = $data->cast();
805-
806-
// Disable Entity casting, because raw primary key data is needed for database.
807-
$data->cast(false);
808-
809-
$primaryKey = $data->{$this->primaryKey};
810-
811-
// Restore Entity casting setting.
812-
$data->cast($cast);
813-
}
814-
815-
// Always grab the primary key otherwise updates will fail.
816-
if (
817-
// @TODO Should use `$data instanceof Entity`.
818-
method_exists($data, 'toRawArray')
819-
&& (
820-
! empty($properties)
821-
&& ! empty($this->primaryKey)
822-
&& ! in_array($this->primaryKey, $properties, true)
823-
&& ! empty($primaryKey)
824-
)
825-
) {
826-
$properties[$this->primaryKey] = $primaryKey;
827-
}
828-
829-
return $properties;
814+
return parent::objectToRawArray($data, $onlyChanged);
830815
}
831816

832817
/**

0 commit comments

Comments
 (0)