Forums / Developer / Fetch attributes from multiple classes

Fetch attributes from multiple classes

Author Message

Håkan Bergman

Monday 04 April 2011 1:17:55 am

Hello,

I am trying to make a fetch on content tree on multiple attributes.

{def $articles = fetch( 'content', 'tree', hash( 'parent_node_id', 2, 'sort_by', array(array( 'priority', false() ), array('attribute', false(), 'article/publish_date'), array('attribute', false(), 'layoutblock/publish_date') ) ) )}

It should first check priority, then publish date on article and then publish date on layouts based on their datatype publish_date which is date and time. It works if I remove one of them but not together. I was looking in the manual and the example there looked like mine, but it wont still work.

Any ideas on how to make this work?

I need to fetch the tree as articles could be on a different depth in the tree based on folders and subfolders and I need to fetch all of them at once.

Best regards,

Håkan Bergman

Philippe VINCENT-ROYOL

Monday 04 April 2011 1:58:03 am

Hello Hâkan,

I had same problem. But its on doc.ez.no. You can't fetch two (or more) differents attributes.

See: http://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Modules/content/Fetch-functions/list

:)

An alternative is to use 2fetches or ezfind.

Cheers

Philippe

Certified Developer (4.1): http://auth.ez.no/certification/verify/272607
Certified Developer (4.4): http://auth.ez.no/certification/verify/377321

G+ : http://plus.tl/dspe
Twitter : http://twitter.com/dspe

Håkan Bergman

Monday 04 April 2011 2:36:23 am

Thank you for your answer.

I was looking at this:

http://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Modules/content/Fetch-functions/list

Re: Multiple attribute sorting ?

{set list_items=fetch_alias( children,hash( 'parent_node_id', $node.node_id, 'sort_by', array( array( 'attribute', false(), 'news/sticky' ), array( 'attribute', false(), 'news/date')), 'limit', $page_limit ) ) }

"Yes, you can sort by multiple attributes using an array of arrays.
This uses fetch_alias but it would be the same for fetch list."

I assumed that fetch content list and tree was sort of same and as this was done with fetch_alias, I hoped it would work with content tree as well. Content tree is bad documented :( so I always look at content list instead.

Can you provide with some code that would do same? I would not want to include ezfind for this particular task. Could you do a fetch for each class (2 in mine case) merge and sort them before output?

like $articles, $layoutblock, merge them, sort the array on their data_map name publish_date and then do the foreach loop and get the desired result.

Philippe VINCENT-ROYOL

Monday 04 April 2011 2:55:12 am

An idea could be :

 {def $articles1 = fetch( 'content', 'tree', hash( 'parent_node_id', 2,  'sort_by', array(array( 'priority', false() ), array('attribute',  false(), 'article/publish_date') ) ) )}
{def $articles2 = fetch( 'content', 'tree', hash( 'parent_node_id', 2,  'sort_by', array(array( 'priority', false() ), array('attribute', false(),  'layoutblock/publish_date') ) ) )}

 {def $mergeArray = $articles1}
 {set $mergeArray = $mergeArray|merge( $articles2 )} 

If you need to sort result, best way is to create an template operator in php which do this :)

Cheers

Philippe

Certified Developer (4.1): http://auth.ez.no/certification/verify/272607
Certified Developer (4.4): http://auth.ez.no/certification/verify/377321

G+ : http://plus.tl/dspe
Twitter : http://twitter.com/dspe

Marko Žmak

Monday 04 April 2011 6:31:07 am

Hakan, there are few things that should be mentioned regarding your question...

1) The best way to sort on attributes from different classes is to create an extended attribute filter that does this.

2) Using attribute filtering and sorting is something that has to be done with much caution, because produces complex SQL queries that can have serious performance impacts. Especially if you use attributes from many different classes. I had some experiences with that...

3) As I can see from your code you are trying to sort on published_date. Note that every eZP object has it's own "published" field which is not an attribute and it's the same for all objects regardless of the object's class. So you can sort ont this field like this:

'sort_by', array( 'published', false() )

Sorting on this field doesn't produce so much complex queries and is better for performance.

4) The published date I mentioned in 3) cannot be changed by editors in the eZP admin interface. And I know that sometimes there is a need for an editor to change the published date (I had similar situations). In this case a better solution is to crate an edit handler that will change this published field based on the date entered in the published_date attribute. I have made en extension that has such functionality:

  • http://projects.ez.no/saedithandlers

Feel free to contact me if you need help in setting it up.

5) I noticed that you are mixing both articles and layoutblocks in the same fetch which seems like mixing apples and oranges, so maybe you should rethink this logic. Of course, I could be wrong about this one, but it's just a tought.

<span class="line"> 'sort_by',        array( 'published', false() </span>

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth