使用 ATK4 中其他模型/阵列中的列扩展 CRUD


Extend CRUD with column from other model/array in ATK4

在atk4中,我喜欢用一列扩展CRUD,该列是模型中可用列数组中的查找。这是为了获取catshop_id已经可用的类别名称(猫店(。但猫店只能作为阵列使用。

该模型为:

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 平台检索的,因此无法加入。

如何用猫店柱延长污垢?到目前为止,我尝试的是使用 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。

你不需要

扩展 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']);
    }
}