Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

filter in ModelAdmin for DATE-Field


2 Posts   1506 Views


Community Member, 83 Posts

4 September 2010 at 2:56am


i have a class with a DATE-Field...

public static $db = array(
'paymentdate' => 'Date',

If the invoice is payed this field will save the date of payment.
If it is NULL it means the invoice hasnt been payed already.

In ModelAdmin I get a Searchfilter as an inputfield for filtering.
For me, it doesnt make sense to have an inputfield for filtering, because i dont want to search for invoices that are payed on a particular day.

I would like to have a dropdownbox with "any", "not payed", "payed" for filtering.

I can make a Dropdownfield for this, but i dont know how to define the filter, that searches for "paymentdate IS NULL"

public function getDefaultSearchContext() {

$context = parent::getDefaultSearchContext();
$paymentMap = array("" => "alle",
//"IS NULL" => "not payed",
"2000-01-01" => "payed");
$paymentField = new DropdownField('paymentdate', 'payed', $paymentMap);
$context->setFilters(array('paymentdate' => new GreaterThanFilter('paymentdate')));

return $context;


Community Member, 83 Posts

9 September 2010 at 8:26am

Ok, so i had to solve it myself.

Here is my new Filter Class HaveValueFilter for anyone who needs this too.

In your function getDefaultSearchContext() write this:

$zahlungMap = array("" => "any",
FALSE => "unpayed",
TRUE => "payed");

$zahlungseingangField = new DropdownField('Zahlungseingang', 'Zahlung', $zahlungMap);
$context->setFilters(array('Zahlungseingang' => new HaveValueFilter('Zahlungseingang')));

Make a new File "mysite/code/HaveValueFilter.php"

class HaveValueFilter extends SearchFilter {
    * @return $query
   public function apply(SQLQuery $query) {
$query = $this->applyRelation($query);
if ($this->value) {
return $query->where(sprintf(
else {
return $query->where(sprintf(
"%s IS NULL",

   public function isEmpty() {
return $this->getValue() == null || $this->getValue() == '';