Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » sorting many many of page within control children

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2088 Views
  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    sorting many many of page within control children Link to this post

    Hello,

    I have a SeriesPage and a SeriesHolder. Each SeriesPage has a manymany set of Features.

    class SeriesPage extends Page {

       ...
       
       static $many_many = array(
        'SeriesFeatures' => 'Features'
       );

    ...

    What I need to do is sort and limit these features. I was able to sort them for the SeriesPage by using the following...

    class SeriesPage_Controller extends Page_Controller {

       function SeriesFeaturesList($limit = null) {

          $data = $this->getManyManyComponents('SeriesFeatures', '', 'DisplayOrder', '', $limit);
          return $data;
       }
    ...

    (DisplayOrder is an int field that I use to sort the items.)

    When I place this in my template for SeriesPage...

    <% control SeriesFeaturesList(7) %>
    $Feature
    <% end_control %>

    all works fine, however when I place it in the SeriesHolder like this...

    <% control Children %>
    ...
    <% control SeriesFeaturesList(7) %>
    $Feature
    <% end_control %>
    ...
    <% end_control %>

    I get nothing, no errors (in dev mode), just nothing displayed.

    Any guidance would be appreciated.

    Thanks,

    Ryan

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: sorting many many of page within control children Link to this post

    If you took out the (7) from the template, does it work again?

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: sorting many many of page within control children Link to this post

    No,

    I tried leaving out the limit and just sorting but I don't get any output in the SeriesHolder page.

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: sorting many many of page within control children Link to this post

    OK, I've got this working now.

    To get it to work I ...

    moved this function

    class SeriesPage_Controller extends Page_Controller {

    function SeriesFeaturesList($limit = null) {

    $data = $this->getManyManyComponents('SeriesFeatures', '', 'DisplayOrder', '', $limit);
    return $data;
    }
    ...

    out of the page controller and into...

    class SeriesPage extends Page {

    ...

    function SeriesFeaturesList($limit = null) {

    $data = $this->getManyManyComponents('SeriesFeatures', '', 'DisplayOrder', '', $limit);
    return $data;
    }

    ...

    Now I can access the function in the SeriesPageHolder template . If anyone could direct me to some documentation that explains why it works I would appreciate it.

    Thanks

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: sorting many many of page within control children Link to this post

    It makes more sense in the model class than the controller. Most likely the controller didn't have the right methods/data to access, so it didn't work - what you've changed is actually probably more correct than what it previously was.

    Usually we have everything in the model, as it's closer to the data layer - but methods can also be called from in template directly from the model.

    I believe if you _did_ want that method in the controller to work correctly, you'd need to call $this->dataRecord->getComponents() instead - which tells it to look for SeriesPage, which is the model (data) class for SeriesPage_Controller. However, I recommend you leave the method you've created in the data class instead.

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: sorting many many of page within control children Link to this post

    Great!

    Thanks for the explanation Sean.

    2088 Views
Page: 1
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.