使用 Laravel 5.1 ,我尝试创建一个动态表格,我可以将多个项目类型与module
相关联。我选择了Polymorphic表,并为每个模块设置了morphable_ids
和morphable_types
:
id module_id morphable_id morphable_type
1 1 1 App/Enemy
2 2 2 App/Enemy
3 3 1 App/Item
我通过关联的tasks
得到了我的模块,又名modules
正在急切地加载任务。当我从API调用深入查看我的任务并检查module_items
时,它会显示文字morphable_id
/ morphable_type
而不是App\Enemy
或App\Item
个实例我在寻找:
more task obj properties...
"module":{
"id" : 1
"module_items":[
{
"id": 3,
"module_id": "3",
"morphable_id": "1",
"morphable_type": "App/Item"
}
]
}
所以我的问题是,如何实际检索多态模型实例(例如,我为模块App\Item
检索1
的{{1}}}?
以下是一些更多信息:
型号:
任务:加载模块
1
模块
class Task extends Model
{
protected $with = ['module'];
public function module()
{
return $this->belongsTo(Module::class);
}
}
多态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);
}
}
模型
ModuleItem
项目的多态关系
class ModuleItem extends Model
{
public function typeable()
{
return $this->morphTo();
}
public function module()
{
return $this->belongsTo(Module::class);
}
}
敌人的多态关系
class Item extends Model
{
public function module_item_types()
{
return $this->morphMany('App\ModuleItem', 'typeable');
}
}
答案 0 :(得分:1)
看看/* actions.js */
export function authenticateClient(client) {
return { type: Types.authenticateClient, client };
}
export function connectClient(client, isNew) {
return { type: Types.connectClient, client, isNew };
}
export function refuseClient(client) {
return { type: Types.refuseClient, client };
}
/* main.js */
/* reducer */
const reducer = (state = loadState(), action) => {
console.log(chalk.red(`*${action.type}*`));
switch (action.type) {
case Actions.Types.authenticateClient:
return authenticateClient(state, action);
case Actions.Types.connectClient:
return connectClient(state, action);
case Actions.Types.refuseClient:
return refuseClient(state, action);
default:
return state;
}
};
/* store */
store = createStore(reducer);
/* app */
store.dispatch(Actions.authenticateClient({username, password}));
:
Illuminate\Database\Eloquent@morphMany
如果您通过/**
* 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);
}
向下跟踪兔子洞,您将看到如果Illuminate\Database\Eloquent@getMorphs
为空,则通过$name
参数推断出数据库表名称。您的列名称被推断为$type
和typeable_id
,而不是typeable_type
和morphable_id
。
修复后,您应该能够访问morphable_type
类似ModuleItem
的类型(或者重命名关系的任何内容)。如果您希望加载类型实例,则需要更新$module_item->typeable
:
ModuleItem