2013-08-12 39 views
0

我有模型:表,列,索引,index_column。關係:獲取雙級深層關係

表1 .. *柱

表1 .. *指數

索引1個.. * index_column在模型表定義

關係:

 'columns' => array(self::HAS_MANY, 'AdColumn', 'table_id'), 
     'indexes' => array(self::HAS_MANY, 'AdIndex', 'table_id'), 

關係在模型列中定義:

 'table' => array(self::BELONGS_TO, 'AdTable', 'table_id'), 
在模型索引定義

關係:在模型index_column定義

 'columns' => array(self:: HAS_MANY, 'AdIndexColumn', 'index_id'), 
     'table' => array(self:: BELONGS_TO, 'AdTable', 'table_id'), 

關係:

 'column' => array(self::BELONGS_TO, 'AdColumn', 'column_id'), 
     'index' => array(self::BELONGS_TO, 'AdIndex', 'index_id'), 
     'table' => array(self::BELONGS_TO, 'AdTable', 'table_id'), 

我需要顯示(使用CGridView)表的列表,每行中應該有表的列的列表和索引列表(名稱+列)。

模型與GII生成,所以我嘗試:

$filter = new AdTable('search'); 
    $filter->unsetAttributes(); // clear any default values 
    $dataProvider = $filter->with('columns', 'indexes')->search(); 

而這種生成的查詢:

  • 用於提取所有的表,
  • 用於取所有列對上述表
  • 用於獲取上表中的所有索引

但是,對於每個索引,還有另一個用於提取索引列的查詢。我喜歡Yii將這些查詢放在一個查詢中。

回答

0

低於你想要的?

$filteredAdTable = AdTable::model()->with(array(
      'indexes' => array(
       'alias' => 'at', 
       //'condition' => '', 
       //'params' => array(), 
       'with' => array(
        'adIndexColumns'=>array(// 'I changed name this relation from columns to adIndexColumns not to make confusion with the relation columns of AdTable' 
         'alias' => 'aic', 
         //'condition' => '', 
         //'params' => array(), 
        ) 

       ) 
      ), 

      'columns' => array(
       'alias' => 'ac', 
       //'condition' => '', 
       //'params' => array(), 
      ) 
     ))->findAll(
      //'condition' => '', 
      //'params' => array(), 
      ); 

也許你應該看一看約together選項

http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/

+0

類似地雷的解決方案:) – koral