我正在尝试使用 Laravel/Jenssegers MongoDB 为我的应用程序设置一些简单的收藏夹
我有两个模型:
class Item {
public function favorites()
{
return $this->hasMany('App\Models\User', 'favorites');
}
}
class User {
public function favorites()
{
return $this->hasMany('App\Models\Item', 'favorites');
}
}
所以一个项目可以有很多用户,一个用户可以有很多项目或“收藏夹”。
当我尝试运行一个简单的查询时:
Item::with('favorites')->get();
收藏夹的关系存在但一个空数组。
该favorites
表很简单,只有三列:
_id, item_id, user_id
我也试过使用embedsMany
没有运气:
return $this->embedsMany('App\Models\User', 'favorites');
我也尝试了各种参数组合。
return $this->embedsMany('App\Models\User', 'favorites', 'building_id', 'user_id');
return $this->embedsMany('App\Models\User', 'favorites', 'user_id', 'building_id');
return $this->hasMany('App\Models\User', 'favorites', 'building_id', 'user_id');
return $this->hasMany('App\Models\User', 'favorites', 'user_id', 'building_id');
使用时的奇怪结果:
return $this->hasMany('App\Models\User', 'favorites', 'building_id', 'user_id');
只有在这种情况下,它才会返回所有用户,即使收藏夹只有一条记录。
还尝试使用getQueryLog
只是给我一个空数组进行调试。
print_r(\DB::connection('mongodb')->getQueryLog());
我已经这样做了mysql
,从来没有遇到过问题。所以不太确定问题出在哪里。