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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

sorting many many of page within control children


Go to End


6 Posts   2528 Views

Avatar
ryanwachtl

Community Member, 46 Posts

2 August 2008 at 5:40am

Edited: 02/08/2008 5:41am

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

Forum Moderator, 922 Posts

4 August 2008 at 7:55pm

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

Avatar
ryanwachtl

Community Member, 46 Posts

5 August 2008 at 5:11am

No,

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

Avatar
ryanwachtl

Community Member, 46 Posts

10 August 2008 at 11:59am

Edited: 10/08/2008 12:00pm

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

Forum Moderator, 922 Posts

10 August 2008 at 12:18pm

Edited: 10/08/2008 12:25pm

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

Community Member, 46 Posts

10 August 2008 at 1:06pm

Great!

Thanks for the explanation Sean.