2016-03-21 13 views
0

Verwenden Laravel 5.1, ich versuche, eine dynamische Tabelle zu erstellen, wo ich mehrere Elementtypen mit einem module verknüpfen kann. Ich ging mit einem Polymorphic Tisch und stellen Sie die morphable_ids und morphable_types pro Modul als solche:Abrufen polymorpher Modellinstanzen

id module_id morphable_id morphable_type 
1  1   1  App/Enemy 
2  2   2  App/Enemy 
3  3   1  App/Item 

ich meine Module über zugehörige tasks bekommen, auch bekannt als modules bestrebt sind, geladen mit Aufgaben. Als ich aus dem API-Aufruf in meine Aufgabe Drill-down, und prüfen Sie module_items, zeigt es die wörtliche morphable_id/morphable_type anstatt die App\Enemy oder App\Item Instanzen ich suchte:

more task obj properties... 
"module":{ 
    "id" : 1 
    "module_items":[ 
     { 
     "id": 3, 
     "module_id": "3", 
     "morphable_id": "1", 
     "morphable_type": "App/Item" 
     } 
    ] 
} 

Also meine Frage ist, wie kann ich tatsächlich die polymorphe Modellinstanz abrufen (wie, ich erhalte App\Item von ID 1 für Modul 1)? Hier

sind einige weitere Informationen:

Modelle:

Aufgabe: Lasten mit Modulen

class Task extends Model 
{ 

    protected $with = ['module']; 


    public function module() 
    { 
     return $this->belongsTo(Module::class); 
    } 
} 

Modul

class Module extends Model 
{ 
    protected $with = ['module_items']; 

    public function task() 
    { 
     return $this->belongsTo(Task::class); 
    } 

    public function module_items() 
    { 
     return $this->hasMany(ModuleItem::class); 
    } 
} 

Polymorphe ModuleItem Modell

class ModuleItem extends Model 
{ 
    public function typeable() 
    { 
     return $this->morphTo(); 
    } 

    public function module() 
    { 
     return $this->belongsTo(Module::class); 
    } 
} 

Polymorphe Beziehung zu Punkt

class Item extends Model 
{ 
    public function module_item_types() 
    { 
     return $this->morphMany('App\ModuleItem', 'typeable'); 
    } 
} 

Polymorphe Beziehung auf Feind

class Enemy extends Model 
{ 
    public function module_item_types() 
    { 
     return $this->morphMany('App\ModuleItem', 'typeable'); 
    } 
} 

Antwort

1

Werfen Sie einen Blick auf Illuminate\Database\[email protected]:

/** 
* Define a polymorphic one-to-many relationship. 
* 
* @param string $related 
* @param string $name 
* @param string $type 
* @param string $id 
* @param string $localKey 
* @return \Illuminate\Database\Eloquent\Relations\MorphMany 
*/ 
public function morphMany($related, $name, $type = null, $id = null, $localKey = null) 
{ 
    $instance = new $related; 

    // Here we will gather up the morph type and ID for the relationship so that we 
    // can properly query the intermediate table of a relation. Finally, we will 
    // get the table and create the relationship instances for the developers. 
    list($type, $id) = $this->getMorphs($name, $type, $id); 

    $table = $instance->getTable(); 

    $localKey = $localKey ?: $this->getKeyName(); 

    return new MorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); 
} 

Wenn Sie den Kaninchenbau folgen nach unten durch Illuminate\Database\[email protected] Sie sehen, dass die DB-Tabellennamen durch den $name Parameter abgeleitet werden, wenn $type null ist. Ihre Spaltennamen werden als typeable_id und typeable_type anstelle von morphable_id und morphable_type gefolgert.

Sobald das behoben ist, sollten Sie auf den ModuleItem Typ wie $module_item->typeable (oder was auch immer Sie die Beziehung umbenennen) zugreifen können. Wenn Sie eifrig Last der Typ-Instanz wollte würden Sie aktualisieren ModuleItem:

class ModuleItem extends Model 
{ 
    protected $with = ['typeable']; 

    public function typeable() 
    { 
     return $this->morphTo(); 
    } 

    public function module() 
    { 
     return $this->belongsTo(Module::class); 
    } 
} 
+0

Guter Punkt, aber wenn ich es ändern, um 'return $ this-> morphMany (ModuleItem :: Klasse,‚typisierbarem‘,‚verwandelbare‘) ; 'um den' type' explizit zu nennen, greift er nicht auf die Instanz – Growler

Verwandte Themen