Example #1
0
 /**
  * Checks to see if any models in the given relation are changed. This function is lazy so will return true as soon
  * as it finds something that has changed.
  *
  * @param Model  $obj
  * @param string $relation
  *
  * @return bool
  */
 protected function relation_changed(Model $obj, $relation)
 {
     // Check that the relation exists
     if ($obj->relations($relation) === false) {
         throw new \InvalidArgumentException('Unknown relation ' . $relation);
     }
     // If the relation is not loaded then ignore it.
     if (!$obj->is_fetched($relation)) {
         return false;
     }
     $relation_object = $obj->relations($relation);
     // Check if whe have a singular relation
     if ($relation_object->is_singular()) {
         // If so check that one model
         return $obj->{$relation}->is_changed();
     }
     // Else we have an array of related objects so start checking them all
     foreach ($obj->{$relation} as $related_model) {
         if ($related_model->is_changed()) {
             return true;
         }
     }
     return false;
 }