Skip to content

Commit

Permalink
fix: update does not work when using custom entity with toRawArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 29, 2023
1 parent 93e9c6b commit 2ecff64
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,7 @@ protected function objectToArray($data, bool $onlyChanged = true, bool $recursiv
*/
protected function objectToRawArray($data, bool $onlyChanged = true, bool $recursive = false): ?array
{
// @TODO Should add an interface for this?
if (method_exists($data, 'toRawArray')) {
$properties = $data->toRawArray($onlyChanged, $recursive);
} else {
Expand Down
9 changes: 6 additions & 3 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,11 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur
{
$properties = parent::objectToRawArray($data, $onlyChanged);

$primaryKey = null;
if ($onlyChanged === false) {
return $properties;
}

$primaryKey = $data->{$this->primaryKey} ?? null;

if ($data instanceof Entity) {
$cast = $data->cast();
Expand All @@ -814,12 +818,11 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur

// Always grab the primary key otherwise updates will fail.
if (
// @TODO Should use `$data instanceof Entity`.
// @TODO Should add an interface for this?
method_exists($data, 'toRawArray')
&& (
! empty($properties)
&& ! empty($this->primaryKey)
&& ! in_array($this->primaryKey, $properties, true)
&& ! empty($primaryKey)
)
) {
Expand Down

0 comments on commit 2ecff64

Please sign in to comment.