1

atk4 では、モデルで使用可能な列の配列のルックアップである 1 つの列で CRUD を拡張するのが好きです。catshop_id がすでに利用可能なカテゴリ名 (catshop) を取得するためです。ただし、catshop は配列としてのみ使用できます。

モデルは次のとおりです。

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) の直前に値をモデルに追加することを考えましたが、これをどのように進めるかはわかりません。

私が探している結果は、catshop 文字列を表示し、catshop 配列からのドロップダウン ビルドで catshop_id を変更できる CRUD です。

4

1 に答える 1

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:モデル内:

別の方法は、モデル内のafterLoadです。

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']);
    }
}
于 2012-05-10T13:28:33.883 に答える