模型管理员数据对象



我有这个类:

class Product extends DataObject { 
    static $db = array(
        'Name' => 'Varchar', 
        'ProductType' => 'Varchar', 
        'Price' => 'Currency'
    ); 
} 

数据库表如下所示:

 --------------------------------- 
| Name      | ProductType | Price |
|-----------+-------------+-------|
| Product 1 | Type1       | 100   |
| Product 2 | Type1       | 240   |
| Product 3 | Type2       | 10    |
| Product 4 | Type1       | 100   |
 --------------------------------- 

我想要 2 个模型管理员:

class MyFirstModel extends ModelAdmin {
    public static $managed_models = array(
        Product
    );
}
class MySecondModel extends ModelAdmin {
    public static $managed_models = array(
        Product
    );
}

得出此结果的最佳方法是什么:

  • MyFirstModel应该向我显示表中ProductType Type1的所有条目

  • MySecondModel应该向我显示表中ProductType Type2的所有条目

getList()应该是答案。文档在这里 http://doc.silverstripe.org/framework/en/reference/modeladmin#results-customization

所以像这样:

public function getList() {
    $list = parent::getList();
    if($this->modelClass == 'Product') {
        $list->exclude('ProductType', 'Type1');
    }
    return $list;
}

如果您有超过 2 个ProductType您可以使用数组来排除多个值,例如

$list->exclude('ProductType', array('Type2', 'Type3'));

最新更新