Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/ronanchilvers/orm
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanchilvers committed Feb 6, 2020
2 parents 8b217f2 + df4c345 commit 4998884
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/Features/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ trait HasRelationships
HasOne,
HasMany;

/**
* Does this model have a relationship for a given field?
*
* @param string $attribute
* @return boolean
* @author Ronan Chilvers <[email protected]>
*/
public function hasRelation($attribute)
{
$method = $this->getRelationMethod($attribute);

return method_exists($this, $method);
}

/**
* Get the relation for a given attribute
*
Expand All @@ -26,12 +40,25 @@ trait HasRelationships
*/
public function getRelation($attribute)
{
$shortAttribute = static::unprefix($attribute);
$method = 'relate' . Str::pascal($shortAttribute);
$method = $this->getRelationMethod($attribute);
if (method_exists($this, $method)) {
return $this->$method();
}

return null;
}

/**
* Create a relationship method for a given attribute name
*
* @param string
* @return string
* @author Ronan Chilvers <[email protected]>
*/
protected function getRelationMethod($attribute)
{
$shortAttribute = static::unprefix($attribute);

return 'relate' . Str::pascal($shortAttribute);
}
}
12 changes: 11 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,14 @@ protected function boot()
*/
public function __isset($attribute)
{
return $this->hasAttribute($attribute);
if ($this->hasAttribute($attribute)) {
return true;
}
if ($this->hasRelation($attribute)) {
return true;
}

return false;
}

/**
Expand Down Expand Up @@ -275,6 +282,9 @@ public function serialize()
public function unserialize($serialized)
{
$this->fromArray(unserialize($serialized));
if ($this->useTimestamps()) {
$this->bootHasTimestamps();
}
$this->boot();
}

Expand Down

0 comments on commit 4998884

Please sign in to comment.