sjdaws / vocal

Extended functionality for Eloquent in Laravel 4 and 5
MIT License
44 stars 5 forks source link

Call to undefined method Illuminate\Database\Query\Builder::saveRelation() #11

Closed chancezeus closed 9 years ago

chancezeus commented 9 years ago

When recursively saving my model an error is thrown:

BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::saveRelation()

The origin of this error is in the saveRelations function at line 795 (version 0.3.1)... The line is: $result = $this->$modelClass()->saveRelation($record);

As far as I understand this tries to call the function "saveRelation" on the relationship defined by "$this->$modelClass()", but this function does not exist. Looking at the laravel API and documentation, there exists a function "save()" which is meant for saving related models (and is similar to associate)...

Any thoughts??

sjdaws commented 9 years ago

saveRelation is a method within Vocal itself (lines 740 to 745), this could mean the model you're trying to save doesn't extend Vocal but rather extends Eloquents Model class.

gbrock commented 9 years ago

Getting the same thing while trying to save a HasOne relationship.

// The important bits of my models
class Party extends Vocal {
    public function organization()
    {
        return $this
            ->hasOne('Organization');
    }

    public function person()
    {
        return $this
            ->hasOne('Person');
    }
}

class Person extends Vocal {
    public function party()
    {
        return $this
            ->belongsTo('Party');
    }
}

class Organization extends Vocal {
    public function party()
    {
        return $this
            ->belongsTo('Party');
    }

}

// then, in the controller...
public function postCreate()
{
    $party = new Party();
    $success = $party->saveRecursive();
    /*
    At this point I get the error:
    BadMethodCallException 
    Call to undefined method Illuminate\Database\Query\Builder::saveRelation()
    */
}
sjdaws commented 9 years ago

Usually when you get a reference to builder it means there is something happening which uses the query builder instead of eloquent, an example of this would be something like:

Party::where('user_id', 4)->saveRecursive();

In you case have you determined the relationships work correctly? If you do:

$test = Party::with('organization', 'person')->find(1);

Do you get a result with $test->organization and $test->person which aren't ids but are Models?

Are you sure that the index columns don't have the same name as the relationships? E.g the party table doesn't have a column named person which holds the id for the person record? If all of the above works, can you try defining your indexes?

public function person()
{
    return $this
        ->hasOne('Person', 'person_id');
}