2016-12-22 8 views
4

私は、リレーショナルモデルの列を通してメインモデルのデータセット全体をソートしようとしています。 MongoのモデルLaravel Jensenggers Eloquentモデルをソートメインモデルとリレーションモデル

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 - 私は

UserEventActivity.php持っている3.1

はここでモデルですLaravel ORM 5.2.43JensenggersにMongoDBを使用しています - 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フィールドに基づいて保存されていません。

私の知る限りのMongoDBはこのように加入をサポートしていません知っているように予想される結果

答えて

1

を達成するために私を助けてください。

周囲の道のりは熱心な負荷を使用することができます。

だからあなた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(); 

全くテストしていないが、行く価値があります。

+0

ありがとう:)それは働いた – Haridarshan