Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing $primaryKey to entity toRawArray() + save() improvements #1772

Closed
wants to merge 14 commits into from
18 changes: 18 additions & 0 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ protected function hasPropertyChanged(string $key, $value = null)
{
return ! (($this->_original[$key] === null && $value === null) || $this->_original[$key] === $value);
}

//--------------------------------------------------------------------

/**
* Gets original property value or $defaultValue specified by parameter if original value is not set OR null
*
* @param string $key
* @param null $defaultValue
*
* @return mixed
*/
public function getOriginalValue(string $key, $defaultValue = null)
{
return $this->_original[$key] ?? $defaultValue;
}

//--------------------------------------------------------------------

/**
* Magic method to allow retrieval of protected and private
Expand Down Expand Up @@ -373,6 +390,7 @@ public function __set(string $key, $value = null)
* attribute will be reset to that default value.
*
* @param string $key
* @throws \ReflectionException
*/
public function __unset(string $key)
{
Expand Down
40 changes: 32 additions & 8 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public function set($key, $value = '', bool $escape = null)
}

//--------------------------------------------------------------------

/**
* A convenience method that will attempt to determine whether the
* data should be inserted or updated. Will work with either
Expand All @@ -458,26 +458,49 @@ public function set($key, $value = '', bool $escape = null)
*/
public function save($data)
{
$originalPrimaryKey = null;
// If $data is using a custom class with public or protected
// properties representing the table elements, we need to grab
// them as an array.
if (is_object($data) && ! $data instanceof \stdClass)
if (is_object($data))
{
$data = static::classToArray($data, $this->primaryKey, $this->dateFormat);
if( ! $data instanceof \stdClass)
{

$originalPrimaryKey = method_exists($data, 'getOriginalValue') ?
$data->getOriginalValue($this->primaryKey) :
($data->{$this->primaryKey} ?? null);

$data = static::classToArray($data, $this->primaryKey, $this->dateFormat);
}
else
{
$originalPrimaryKey = $data->{$this->primaryKey} ?? null;
}

}
else if(is_array($data))
{
$originalPrimaryKey = $data[$this->primaryKey] ?? null;
}

if (empty($data))
// If originalPrimaryKey is not null there is only one item in $data array and
// $data[$primaryKey] = $originalPrimaryKey
// then nothing has been changed in other case someone wants to update only primaryKey value.
// We do not have possibilities to check object here :(
if(empty($data) || ($originalPrimaryKey !== null && is_array($data) && ! empty($data[$this->primaryKey]) && count($data) === 1 && $data[$this->primaryKey] === $originalPrimaryKey))
{
return true;
}

if (is_object($data) && isset($data->{$this->primaryKey}))
if (is_object($data) && !empty($originalPrimaryKey))
{
$response = $this->update($data->{$this->primaryKey}, $data);
$response = $this->update($originalPrimaryKey, $data);
}
elseif (is_array($data) && ! empty($data[$this->primaryKey]))
// Adding support for UPDATE table SET primaryKey = 'newVal' WHERE primaryKey = 'oldVal'
elseif (is_array($data) && ! empty($originalPrimaryKey))
{
$response = $this->update($data[$this->primaryKey], $data);
$response = $this->update($originalPrimaryKey, $data);
}
else
{
Expand All @@ -487,6 +510,7 @@ public function save($data)
return $response;
}


/**
* Takes a class an returns an array of it's public and protected
* properties as an array suitable for use in creates and updates.
Expand Down