Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » ModelAdmin + searchable_fields + dropdown

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 3092 Views
  • danzzz
    Avatar
    Community Member
    175 Posts

    ModelAdmin + searchable_fields + dropdown Link to this post

    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

  • Zauberfisch
    Avatar
    Community Member
    30 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

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

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

    no sorry, this results in

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

  • Zauberfisch
    Avatar
    Community Member
    30 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

    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.

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

    hi zauberfisch,

    thank you, waiting for your results on that

  • moloko_man
    Avatar
    Community Member
    72 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

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

  • Zauberfisch
    Avatar
    Community Member
    30 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1786 Posts

    Re: ModelAdmin + searchable_fields + dropdown Link to this post

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

    3092 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.