【问题标题】:ModelAdmin DataObjects模型管理数据对象
【发布时间】:2013-09-10 09:15:15
【问题描述】:

我有这门课:

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 应该显示表中ProductTypeType1 的所有条目

  • MySecondModel 应该显示表中ProductTypeType2 的所有条目

【问题讨论】:

    标签: silverstripe modeladmin


    【解决方案1】:

    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'));
    

    【讨论】:

    • 谢谢!添加新产品时:如何确保在 FirstModel 中添加新产品时将值 Type1 保存在 ProductType 行中,而在 SecondModel 中添加产品时,将保存值 Type2?
    • 这会有点复杂,而且不是直接开箱即用的。您可能想查看ModelAdmin 上的重载getEditForm() 并查看GridField 上的组件,也许是GridFieldAddNewButton。或者检查您的 DataObject 的getCMSFields() 中的 URL/Referrer。这里没有承诺,你必须环顾四周......
    猜你喜欢
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2012-06-26
    相关资源
    最近更新 更多