Jump to:

23369 Posts in 18144 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » Model Admin - so many questions, so little documentation

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2 3
Go to End
Author Topic: 7851 Views
  • EzraNaj
    Avatar
    Community Member
    11 Posts

    Re: Model Admin - so many questions, so little documentation Link to this post

    Hi swaiba,

    I already found a solution.
    I already have a class ItemAdmin extending ModelAdmin.
    I created a class ItemAdmin_CollectionController extending ModelAdmin_CollectionController class.

    Then I override ModelAdmin_CollectionController class by setting $collection_controller_class under class ItemAdmin
    ==
    public static $collection_controller_class ="ItemAdmin_CollectionController"
    ==

    I overloaded getSearchQuery() >>
    =====
    function getSearchQuery( $searchCriteria ) {

          $query = parent::getSearchQuery( $searchCriteria )->leftJoin( 'Category', 'Category.ID = Product.CategoryID' );
          return $query->orderby( 'Category.Name, Product.Name ASC' );

    }
    =====

    It worked!

    There's also another way though. You can extend TableListField and set $resultsTableClassName to class name you created.
    You can then overload function getQuery() like this >>
    ===
    function getQuery() {
       $this->customQuery->leftJoin( 'Category', 'Category.ID = Product.CategoryID');
       $this->customQuery->orderby( 'Category.Name, Product.Name ASC' );
    return parent::getQuery();
    }
    ===

    I hope this helps to everyone who will encounter this issue.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Model Admin - so many questions, so little documentation Link to this post

    nice one! that is a very simple example - thanks for posting the solution

    this has worked very well for what I wanted, I did also add a condition to ensure that the extra condirtion is only applied to the intended managed model...

    if ($this->modelClass == 'DataObjectName')
    {
    $query->where[] = "myfield = xyz";
    }

    7851 Views
Page: 1 2 3
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.