Skip to content

Commit d5a85e3

Browse files
committed
fix: Entity's primary key is cast when inserting
1 parent 4b7165e commit d5a85e3

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

system/Model.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,17 +788,32 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur
788788
{
789789
$properties = parent::objectToRawArray($data, $onlyChanged);
790790

791+
$primaryKey = null;
792+
793+
// For Entity
794+
if (method_exists($data, 'cast')) {
795+
$cast = $data->cast();
796+
797+
// Disable Entity casting, because raw primary key data is needed for database.
798+
$data->cast(false);
799+
800+
$primaryKey = $data->{$this->primaryKey};
801+
802+
// Restore Entity cast setting.
803+
$data->cast($cast);
804+
}
805+
791806
// Always grab the primary key otherwise updates will fail.
792807
if (
793808
method_exists($data, 'toRawArray')
794809
&& (
795810
! empty($properties)
796811
&& ! empty($this->primaryKey)
797812
&& ! in_array($this->primaryKey, $properties, true)
798-
&& ! empty($data->{$this->primaryKey})
813+
&& ! empty($primaryKey)
799814
)
800815
) {
801-
$properties[$this->primaryKey] = $data->{$this->primaryKey};
816+
$properties[$this->primaryKey] = $primaryKey;
802817
}
803818

804819
return $properties;

0 commit comments

Comments
 (0)