Skip to content
Open
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
19 changes: 13 additions & 6 deletions src/Model/Traits/HasCompositePrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ public function getKey()
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*
* @throws \Exception
*/
protected function setKeysForSaveQuery(Builder $query)
{
foreach ($this->getKeyName() as $key) {
if (isset($this->$key))
$query->where($key, '=', $this->$key);
else
throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);
if (isset($this->original[$key])) {
// keyed based on original value, allow for changing primary key on save
$query->where($key, '=', $this->original[$key]);
} else if (isset($this->attributes[$key])) {
// keyed based on original value, allow for changing primary key on save
$query->where($key, '=', $this->attributes[$key]);
} else {
throw new Exception(__METHOD__.'Missing part of the primary key: '.$key);
}
}

return $query;
Expand All @@ -60,7 +67,7 @@ protected function setKeysForSaveQuery(Builder $query)
*/
public static function find($ids, $columns = ['*'])
{
$me = new self;
$me = new static;
$query = $me->newQuery();

foreach ($me->getKeyName() as $key) {
Expand All @@ -81,7 +88,7 @@ public static function find($ids, $columns = ['*'])
*/
public static function findOrFail($ids, $columns = ['*'])
{
$result = self::find($ids, $columns);
$result = static::find($ids, $columns);

if (!is_null($result)) {
return $result;
Expand Down