laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.2k stars 10.89k forks source link

Polymorphic relations: eager loading from "morpher(?)" #1939

Closed tomsseisums closed 11 years ago

tomsseisums commented 11 years ago

I'm pretty new to polymorphic relations, so, might be that I'm just imagining things, anyways, "morpher":

class Value extends Eloquent
{
    protected $table = 'values';

    /**
     * Get the valuable target
     */
    public function valuable()
    {
        return $this->morphTo();
    }
}

One of "morphables":

class Chequenumber extends Eloquent
{
    protected $table = 'chequenumbers';

    public function value()
    {
        return $this->morphOne('Value', 'valuable');
    }
}

When I:

Chequenumber::with('value')->get();

Everything goes smoothly, everything is eager loaded, no problems.

Though, when I try to eager load the other-way around:

Value::with('valuable')->get();

I end up with a _Whoops!_:

Symfony \ Component \ Debug \ Exception \ FatalErrorException Class name must be a valid object or a string

open: Z:\laravel\youdo\bootstrap\compiled.php

public function belongsTo($related, $foreignKey = null)
{
    list(, $caller) = debug_backtrace(false);
    $relation = $caller['function'];
    if (is_null($foreignKey)) {
       $foreignKey = snake_case($relation) . '_id';
    }
    $instance = new $related(); // line that throws error
    $query = $instance->newQuery();
    return new BelongsTo($query, $this, $foreignKey, $relation);

It feels buggy, because, if I:

$values = Value::all();

foreach ($values as $value)
{
    var_dump($value->valuable);
}

it works.

values table:

+------+---------------+-------------------+
|  id  |  valuable_id  |   valuable_type   |
+------+---------------+-------------------+
|    8 |             1 | Placeholder       |
|    9 |             2 | Placeholder       |
|   10 |             1 | Chequenumber      |
|   11 |             1 | Product           |
|   12 |             2 | Product           |
|   13 |             3 | Product           |
|   14 |             4 | Product           |
+------+---------------+-------------------+

Am I doing something wrong, or is it a bug?

usirin commented 11 years ago

+1

I did the same yesterday and it's buggy, i guess. Because when i try to do,

$model->load('foo', 'bar');

it's working. I don't know how to solve this. But i was planning to open an issue. But @joltmode did it before me.

taylorotwell commented 11 years ago

It is currently not possible to eager load from the morphed object.

vedmant commented 9 years ago

It would be great if it will throw error like "Can't eager load from the morphed object" when trying to add morphed relationship to function with(). Thanks!

BelalAmin commented 7 years ago

what is the current status of this issue, is it still not possible to eager load from the morphed object ?

kurucu commented 6 years ago

I assume the reason is that it can't tell which models to query morphables for? Perhaps in the model it would be possible to list an array of morphTo models, so laravel knew what to query?