Skip to content

Commit

Permalink
Add basic comparison method
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanchilvers committed May 2, 2021
1 parent 167f431 commit 011bfc0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,38 @@ protected function getQueryBuilderInstance()

/* Persistance methods **************/
/************************************/

/************************************/
/** Comparison methods **************/

/**
* Is this model equal to another one?
*
* For a model to be equal to another it must:
* - Be loaded
* - Be of the same class
* - Have the same id
*
* @param Ronanchilvers\Orm\Model $model
* @return bool
* @author Ronan Chilvers <[email protected]>
*/
public function equals(Model $that): bool
{
if (!$this->isLoaded() || !$that->isLoaded()) {
return false;
}
if (get_class($that) !== get_called_class()) {
return false;
}
$thisId = $this->getAttributes()[static::primaryKey()];
$thatId = $that->getAttributes()[static::primaryKey()];
if ($thisId != $thatId) {
return false;
}

return true;
}

/************************************/
}

0 comments on commit 011bfc0

Please sign in to comment.