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.

Data Model Questions /

ModelAdmin + searchable_fields + dropdown

Go to End

10 Posts   3426 Views


Community Member, 175 Posts

14 March 2011 at 5:13am

Edited: 19/05/2011 10:02am


class Program extends DataObject {


static $has_one = array(
   'Acount' => 'Account'

static $belongs_many_many = array(
'Categories' => 'CategoryPage',

static $searchable_fields = array (
'program_id' => array(
    'title' => 'Program ID',
    'field' => 'NumericField',
    'filter' => 'PartialMatchFilter'
'name' => array('title'=>'Programmname'),
   'URLSegment' => array('title'=>'URL Segment'),
'description' => array('title'=>'Beschreibung'),

   'Categories.ID' => array('title' => 'Kategorie'),
   'Account.ID' => array('title'=>'Account')

I have some problems with the 2 last entries in searchable_fields. For the categories I get dropdownfield with all Categories, but if I perform a search it results in an sql error. It only works if I use "Categories.Name", but then I get a textfield instead a dropdown.

Error: "Couldn't run query: SELECT DISTINCT count(*) FROM "Program" INNER JOIN "CategoryPage_Programs" AS "CategoryPage_Programs"
ON "CategoryPage_Programs"."ProgramID" = "Program"."ID" LEFT JOIN "SiteTree" AS "SiteTree"
ON "CategoryPage_Programs"."CategoryPageID" = "SiteTree"."ID" WHERE ("CategoryPage"."ID" IN ('21'))
Unknown column 'CategoryPage.ID' in 'where clause'" at line 525 of /var/www/xxx/sapphire/core/model/MySQLDatabase.php

And the "Account.ID" also returns a dropdown as expectet, but the dropdown shows me only the IDs instead of the Account Names.

Whats wrong here?



Community Member, 30 Posts

5 May 2011 at 3:03am

instead of 'Categories.ID' => array('title' => 'Kategorie') you have to use 'CategoriesID' => array('title' => 'Kategorie')
I think


Community Member, 175 Posts

6 May 2011 at 12:44am

no sorry, this results in

[User Error] Uncaught Exception: Unable to traverse to related object field [CategoriesID] on [Program]


Community Member, 30 Posts

6 May 2011 at 2:39am

Oh, sorry, I was should have read your post more carefully, i thought you have a has_many relation, in fact it seems that it is a many_many.
nvm what I said then.

I never did filtering on a many_many but actually I will run into that problem tomorrow or maybe on Monday, I will post again after I had my try on it.


Community Member, 175 Posts

6 May 2011 at 2:58am

hi zauberfisch,

thank you, waiting for your results on that :-)


Community Member, 72 Posts

27 May 2011 at 10:58am

Any luck on this issue, I'm having the same problem. Both with a has_one and a has_many relationship.


Community Member, 30 Posts

27 May 2011 at 7:19pm

hello, sorry that it is taking me that long, i still have the problem, and i still want to find a way, but it needs to wait for I have a couple of other things with higher priority.

filtering on a has_one is really simple, here an example class:

class Hotel extends DataObject {
   static $db = array(
      'Title' => 'Text'
   static $has_one = array(
      'State' => 'State'
   static $searchable_fields = array(
      'Title' => array('title' => 'Name'),
      'StateID' => array(
         'title'=>'State Name'


Forum Moderator, 1805 Posts

27 May 2011 at 9:04pm

Edited: 27/05/2011 9:08pm

I have hacked up getSearchQuery($searchCriteria) in my ModelAdmin_CollectionController to handle custom search requirements. I sometimes need to add/modify the fields in the DataObject's scaffoldSearchFields() as well. It's not perfect, but it's not that bad and gets the job done easily.


function getSearchQuery($searchCriteria) {
   $query = parent::getSearchQuery($searchCriteria);
   if ($this->modelClass == 'MyDataObject'){
      $query->where[] = 'EXISTS (SELECT 1 FROM SomeTable st'
            .' WHERE MyDataObject.ID = st.MyDataObjectID'
            .' AND st.Field='.$searchCriteria['MyFieldFromSearchFormFields'].')';
   return $query;

Go to Top