ModelAdmin DataObjects

754 views Asked by At

I have this class:

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

The database table looks as follows:

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

I would like to have 2 model admins:

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

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

What is the best way to come to this result:

  • MyFirstModel should show me all entries where ProductType in table is Type1

And

  • MySecondModel should show me all entries where ProductType in table is Type2
1

There are 1 answers

2
colymba On BEST ANSWER

getList() should be the answer. DOC here http://doc.silverstripe.org/framework/en/reference/modeladmin#results-customization

So something like:

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

If you have more than 2 ProductType you can use an array to exclude multiple values like

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