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

eZ debug

Timing: Jan 18 2025 01:02:14
Script start
Timing: Jan 18 2025 01:02:14
Module start 'content'
Timing: Jan 18 2025 01:02:15
Module end 'content'
Timing: Jan 18 2025 01:02:15
Script end

Main resources:

Total runtime1.4838 sec
Peak memory usage4,096.0000 KB
Database Queries204

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0079 588.9297180.8359
Module start 'content' 0.00791.3299 769.7656631.2344
Module end 'content' 1.33780.1459 1,401.0000341.3984
Script end 1.4837  1,742.3984 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00430.2873210.0002
Check MTime0.00160.1045210.0001
Mysql Total
Database connection0.00090.059510.0009
Mysqli_queries1.385193.35092040.0068
Looping result0.00210.14422020.0000
Template Total1.449197.720.7245
Template load0.00270.182520.0014
Template processing1.446497.479020.7232
Template load and register function0.00020.011210.0002
states
state_id_array0.00180.123510.0018
state_identifier_array0.00160.108220.0008
Override
Cache load0.00240.1625540.0000
Sytem overhead
Fetch class attribute can translate value0.00140.094940.0004
Fetch class attribute name0.00120.079470.0002
XML
Image XML parsing0.00200.132540.0005
class_abstraction
Instantiating content class attribute0.00000.001180.0000
General
dbfile0.00410.2747360.0001
String conversion0.00000.000730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
12content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1content/datatype/view/ezxmltags/header.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/header.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/li.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/li.tplEdit templateOverride template
1content/datatype/view/ezxmltags/ul.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/ul.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 31
 Number of unique templates used: 11

Time used to render debug report: 0.0002 secs