2013-04-10 29 views
0

如何創建「一對多」關係的搜索表單/標準:讓我們說「選擇所有人羣> 10.000」。在Yii中搜索MongoDB文檔中的條件

我使用MongoRecord擴展名(http://www.yiiframework.com/extension/mongorecord/)作爲MongoDb的基本活動記錄。

我創建了一個基於來自mongo數據庫(它適用於我)的集合Yii的GridView。 我document`s集合中的一個具有這樣的結構:

{ 
_id":1, 
"teamId":2453, 
"teamName":"Team A", 
"competition":["Competition A","Competition B"], 
"matches": 
    [ 
     { 
      "_id":147852, 
      "crowd":10234, 
      "yellowCards":2, 
      "scorers" [{....}] 
      .... 
     } 
    ] 
... 
} 

我的文檔:

$teams = MongoTeam::model()->findAll();  

My dataProvider is: 
$dataProvider=new CArrayDataProvider($teams, array(
     'id'=>'_id', 
     'sort'=>array(
      'attributes'=>array(
       '_id', 'teamId', 'teamName', 'matches', ... 
      ), 
     ), 
     'pagination'=>array(
      'pageSize'=>20, 
     ), 
    )); 
    $this->render('index', array('dataProvider'=>$dataProvider)); 

並在視圖我有這樣的事情:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 
    'columns'=>array(
     array(
      'name' => 'Team ID',   
      'type' => 'raw', 
      'value' => 'CHtml::encode($data->teamID)' 
     ), 
     array(
      'name' => 'Team Name',   
      'type' => 'raw', 
      'value' => 'CHtml::encode($data->teamName)' 
     ), 
     ... 

但我不能創建具有子文檔條件的過濾器(我的情況:「匹配」)。我不想使用任何其他mongoDB擴展/模塊等。 感謝和抱歉我的英語不好。

+0

MongoReord多年來一直沒有維護,因此我不確定這裏的任何人是否可以幫助您編程,您最好使用更完整的擴展。 – Sammaye 2013-04-10 21:50:24

+0

我看到了,但它是我所需要的(當然,我對它做了一些修改) – Larry 2013-04-10 21:58:37

+0

要過濾你需要一個函數或一個類,它可以接受傳入的GET,過濾它並應用過濾器,你還沒有得到了足夠的代碼,甚至沒有用於根文檔過濾。 – Sammaye 2013-04-10 22:28:22

回答

0
$query=array(
    'matches.crowd'=>array('$gt'=>10000), 
    'matches.$'=>1 
); 
$teams = MongoTeam::model()->findAll($query); 

你可以這樣使用。但是你不能使用這樣的數據提供者。因爲它會在$ teams var中產生太多的行。您必須使用數據提供者的標準。我正在用YiiMongoDbSuite這樣做,它有EMongoCriteria和EMongoDataProvider類來完成這個。