Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense.
$this->primaryKey is the column name like id.
$properties is an array with property_name => value.
If id is in the array values, the condition is false.

I thought it was a mistake, and it should use array_key_exists(), but
when the primary key value is binary data and it is cast to a string by Entity casting,
we need to set raw binary data even when there is a string primary key value in $properties.

Without casting, if there is a primary key value in $properties, we don't need to set it again,
but setting the same value is no harm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh.. yes, that makes sense. Thanks.

&& ! empty($primaryKey)
)
) {
Expand Down