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
Reply


10 Posts   3440 Views

Avatar
danzzz

Community Member, 175 Posts

14 March 2011 at 5:13am

Edited: 19/05/2011 10:02am

hi,

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?

thx

Avatar
Zauberfisch

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

Avatar
danzzz

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]

Avatar
Zauberfisch

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.

Avatar
danzzz

Community Member, 175 Posts

6 May 2011 at 2:58am

hi zauberfisch,

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

Avatar
moloko_man

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.

Avatar
Zauberfisch

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

Avatar
swaiba

Forum Moderator, 1808 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.

e.g.

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