I've got a generic model called Item
and a related model called Itemproperties
.
Item -> hasOne -> Itemproperties
Itemproperties -> belongsTo -> Item
The Item
model has a property called 'itemtype_id' which tells the model which table to store to. Ex: itemtype = 1, then table is itemproperties_1 for itemtype = 2, then table is itemproperties_2.
So I changed the getTable() method on the Itemproperties model:
function getTable(){
return 'item_properties_' . $this->itemtype_id;
}
So, when creating a new item and its properties, I've been setting up the Itemproperties like this:
// Create & Save the Item
$item = new Item(['itemtype_id' => $itemtype_id]);
$item->save();
// Save the Properties
$itemProperties = new ItemProperties($properties);
$itemProperties->itemtype_id = $item->itemtype_id;
$item->properties()->save($itemProperties);
This seems to work fine... when saving the Itemproperties.
However, when I try to retrieve them, by doing $item->properties
, I don't have a way to tell it the itemtype_id, so it can't find the correct table.
My question: Is there a way to set up the eloquent relationship so that I can pass the item->itemtype_id to the Itemproperties so it knows which table to use?
Or is there a better overall strategy to accomplish what I'm doing using different tables for different item types?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2w48n1R
via IFTTT
Aucun commentaire:
Enregistrer un commentaire