Skip to content

Commit

Permalink
Merge pull request laravel-ardent#258 from samuel-cloete/master
Browse files Browse the repository at this point in the history
In `Ardent\belongsTo` $relation was being over-written with the calling function name
  • Loading branch information
igorsantos07 committed May 10, 2015
2 parents 20fc03a + 2a5e554 commit b2298eb
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/LaravelBook/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,15 @@ public function __call($method, $parameters) {
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = NULL, $otherKey = NULL, $relation = NULL) {
$backtrace = debug_backtrace(false);
$caller = ($backtrace[1]['function'] == 'handleRelationalArray')? $backtrace[3] : $backtrace[1];

// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
$relation = $caller['function'];
if (is_null($relation)) {
$backtrace = debug_backtrace(false);
$caller = ($backtrace[1]['function'] == 'handleRelationalArray')? $backtrace[3] : $backtrace[1];
$relation = $caller['function'];
}

if (is_null($foreignKey)) {
$foreignKey = snake_case($relation).'_id';
Expand Down

0 comments on commit b2298eb

Please sign in to comment.