我试图通过关系模型的列对主模型的整个数据集进行排序。我正在使用Laravel ORM 5.2.43和Jensenggers MongoDb 3.1
这是我的模特
UserEventActivity.php - Mongo模型
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class UserEventActivity extends Eloquent
{
protected $collection = 'user_event_activity';
protected $connection = 'mongodb';
public function handset() {
return $this->hasOne('HandsetDetails', '_id', 'handset_id');
}
public function storeDetail() {
return $this->hasOne('StoreDetails', 'st_id', 'store_id');
}
}HandsetDetails.php - Mongo模型
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class HandsetDetails extends Eloquent
{
var $collection = 'user_handset_details';
var $connection = 'mongodb';
}StoreDetails.php - MySql模型
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Illuminate\Database\Eloquent\Model as Eloquent;
class StoreDetails extends Eloquent
{
use HybridRelations;
protected $connection = 'mysql';
protected $table = 'icn_store';
}Php脚本
$activity = UserEventActivity::join('handset ', 'handset._id', '=', 'handset_id')
->join('storeDetail', 'store_id', '=', 'storeDetail.st_id')
->orderBy('handset.handset_make', 'desc')
->select('storeDetail.*', 'handset.*')
->get()
->toArray();来自UserEventActivity的数据不是基于手机关系中的handset_make字段存储的。
请帮助我达到预期的效果。
发布于 2016-12-29 09:59:49
据我所知,MongoDB不支持这样的联接。
绕过它的一种方法是使用急切的加载。
因此,您的UserEventActivity模型可能如下所示:
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class UserEventActivity extends Eloquent
{
protected $collection = 'user_event_activity';
protected $connection = 'mongodb';
public function handset() {
return $this->hasOne('HandsetDetails', '_id', 'handset_id');
}
public function storeDetail() {
return $this->hasOne('StoreDetails', 'st_id', 'store_id');
}
public function getHandsetMakeAttribute()
{
return $this->handset->handset_make;
}
}注意getHandsetMakeAttribute()访问器。这样你就可以打电话了:
$activity = UserEventActivity::with('storeDetail')
->with('handset')
->get()
->sortByDesc('handset_make')
->toArray();一点也不测试,但值得一试。
https://stackoverflow.com/questions/41279820
复制相似问题