-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of ssh://github.com/ronanchilvers/orm
- Loading branch information
Showing
2 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters