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.

Archive

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

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

sorting many many of page within control children


Reply

6 Posts   2099 Views

Avatar
ryanwachtl

2 August 2008 at 5:40am (Last edited: 2 August 2008 5:41am), Community Member, 46 Posts

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

Avatar
Sean

4 August 2008 at 7:55pm Forum Moderator, 921 Posts

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

Avatar
ryanwachtl

5 August 2008 at 5:11am Community Member, 46 Posts

No,

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

Avatar
ryanwachtl

10 August 2008 at 11:59am (Last edited: 10 August 2008 12:00pm), Community Member, 46 Posts

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

Avatar
Sean

10 August 2008 at 12:18pm (Last edited: 10 August 2008 12:25pm), Forum Moderator, 921 Posts

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.

Avatar
ryanwachtl

10 August 2008 at 1:06pm Community Member, 46 Posts

Great!

Thanks for the explanation Sean.