2012-05-08 36 views
1

在atk4中,我喜歡將CRUD擴展爲一列,這是查找模型中可用列的數組。這是要獲得catshop_id已經可用的類別名稱(catshop)。但貓店只能作爲數組使用。使用ATK4中的其他模型/數組的列擴展CRUD

的模型是:

class Model_CatLink extends Model_Table { 
    public $table='catlink'; 
    function init() { 
    parent::init(); 
    $this->addField('catshop_id'); 
    $this->addField('margin_ratio'); 
    } 
} 

在頁面上我有:

$catshop=array(1=>'cat1',2=>'another cat 2',...,123=>'top cat'); 
$c=$p->add('CRUD'); 
$m=$this->add('Model_CatLink'); 
$c->setModel($m); 

現在網格顯示catshop_id和margin_ratio領域。用catshop_id我想查找$ catshop中可用的類別標題。這個$ catshop數組實際上是從另一個mysql平臺檢索的,因此無法加入。

如何擴展catshop列的crud? 我到目前爲止所嘗試的是用addExpression擴展模型本身......無法讓它工作。

我想是這樣的,首先把它添加到模型:

$self=$this; 
$this->addExpression('catshop')->set(function($select) use ($self){ 
    return $self->catshop[$self->get('catshop_id')]; 
}); 

然後在頁面上的$ catshop傳遞給模型:

$catshop=array(1=>'cat1',2=>'another cat 2',...,123=>'top cat'); 
$c=$p->add('CRUD'); 
$m=$this->add('Model_CatLink'); 
$m->catshop=$catshop; 
$c->setModel($m); 

然後,我認爲在$ c-> setModel($ m)之前將值添加到模型中,儘管我不確定如何繼續此操作。

我正在尋找的結果是一個CRUD,它顯示了catshop字符串,並且還允許通過catshop數組的下拉構建來更改catshop_id。

回答

0

您不需要擴展CRUD,您需要擴展CRUD正在使用的網格。或者,您可以擴展模型加載。

選項1:在電網:

class MyGrid extends Grid { 
    function init(){ 
     parent::init(); 
     $this->add('myfield','category'); 
    } 
    funciton format_myfield($field){ 
     $this->current_row[$field]= 
      $this->model->lookupCategory($this->current_row[$field]); 

     // use current_row_html[] if you want HTML output 
    } 
} 

然後當你創建CRUD需要指定這樣的:

$c=$this->add('CRUD',array('grid_class'=>'MyGrid')); 

選項2:模型:

你的選擇是內部負荷型號:

class Model_CatLink extends Model_Table { 
    function init(){ 
     parent::init(); 

     $this->addExpression('category')->set('undefined'); // nothing by default 

     $this->addHook('afterLoad',$this); 
    } 
    function afterLoad(){ 
     $this['category']=$this->lookupCategory($this['category_id']); 
    } 
} 
+0

我在學t他的選擇。選項1將解決它作爲格式,而選項2已經解決它在模型中。在添加/修改記錄時如何獲取CRUD格式部分的下拉列表?使用hasMany()它會自動執行這兩個操作。這裏的網格部分已經解決了,所以我應該在$ c->表單上自己做addField('dropdown','category')還是應該考慮擴展Field_Reference? –

+1

要將下拉列表添加到CRUD的表單部分,我使用$ m-> getField('catshop_id') - >數據類型('list') - > setValueList($ catshop);它正在更新數據庫。現在我將嘗試使用建議的選項1在網格中列出相應的類別。 –

相關問題