eZ Community » Forums » Developer » How to fetch related objects and...
expandshrink

How to fetch related objects and reverse related objects.pragrammaticaly in plain php

How to fetch related objects and reverse related objects.pragrammaticaly in plain php

Thursday 05 January 2012 4:58:37 pm - 2 replies

Hi everyone

 

For doing this in an ez template we could use these fucntions to do that:

related objects:

 fetch( 'content', 'related_objects',
         hash( 'object_id',            object_id,
             [ 'attribute_identifier', attribute_identifier, ]
             [ 'all_relations',        boolean,              ]
             [ 'group_by_attribute',   boolean,              ]
             [ 'sort_by',              sort_by,              ]
             [ 'limit',                limit,                ]
             [ 'offset',               offset,               ]
             [ 'as_object',            boolean,              ]
             [ 'load_data_map',        boolean               ] ) )

and

 reverse related objects:

 fetch( 'content', 'reverse_related_objects', 
       hash( 'object_id',            object_id, ....) )
           [ 'attribute_identifier', attribute_identifier, ]
           [ 'all_relations',        boolean,              ]
           [ 'group_by_attribute',   boolean,              ]
           [ 'sort_by',              sort_by               ]
           [ 'ignore_visibility',    ignore_visibility     ]
           [ 'limit',                limit                 ]
           [ 'offset',               offset                ]
           [ 'as_object',            as_object             ]
           [ 'load_data_map',        load_data_map         ] ) )

 

 

But is there any way to do this 2 fucntion in plain old php

 

Cause i'm doing a php cron batch who absolutely needs these 2 functions !

Without faith, I've made the following try

eZOperationHandler::execute( 'content', 'related_objects', array( ....))

and

eZOperationHandler::execute( 'content', 'reverse_related_objects', array( ....))

And of course It didn't work...

 

Any idea how to do this..?

Thursday 05 January 2012 6:02:40 pm

Hello Trash Can,

 

Welcome to the eZ Community!

 

This thread reminds me of a classic comment by Kristian Hole on Łukasz blog with a very clear example. Sorry it's not directly linkable (the comment, but see the article that Chris shares in the following post).

 

"The same ways as in templates"

It seems your calling the wrong class which I think is why your examples do not work so far, You prolly want to try using the following class and method to perform the desired fetches. This is the exact same code that all default template fetch functions use, so it's perfect for your needs.

eZFunctionHandler::execute

Here is an example of using the same kind of fetches as supported within templates. Remember when you do use this class / style of fetches you do sacrifice a tiny bit of speed.

You can copy and paste from templates when building code using this class, just remember to use array instead of hash. I think it would be helpful to try ... Sry, I did not post examples earlier.

$nodeObjectID = 42042;
$nodes = eZFunctionHandler::execute( 'content', 'related_objects', array(
                                                              'object_id', $nodeObjectID,
                                                              'all_relations', true(),
                                                              'group_by_attribute', true(),
                                                              'sort_by', array( 
                                                                               array( 'class_name', true() ),
                                                                               array( 'name', true()  ) ) ) ); 
$nodes = eZFunctionHandler::execute( 'content', 'reverse_related_objects', array(
                                                              'object_id', 42 ) );

 

Remember that the syntax used in the above example is the exact same as used within all templates today, except one key difference ... you - do not - use 'hash' to create the associative arrays as you would in templates but rather PHP's built-in array function instead.

 

Template Fetch documentation

If your looking to learn more about the supported fetch function(s) parameter syntax supported,
It clearly shows what you can use and how specifically to use them, then have a read of some of the following related pages of documentation ...

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

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

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

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

 

If your looking to learn more about the fetch functions you can use and how specifically to use them, then have a read of some of the following related pages of documentation available on this page ...

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

If you seek validation from other users, listen to some very smart folks who recently shared some advice on subject specifically and the key info you would need to get started using / finding these tools. The thread talks about the usage your looking for (I think...) in greater detail.

It covers how you can use fetch function calls using template style parameter syntax for the fetch parameters and or use the actual base PHP object class methods which are used indirectly (in the end to do the actual work of the fetch is done using the PHP style based on the provided template style fetch parameters (translated of sorts to the PHP style) when you use a template style fetch.

http://share.ez.no/forums/developer/reverse-relation_list-from-php#comment72060

http://share.ez.no/forums/developer/reverse-relation_list-from-php

 

I do hope that his post helps other users find similar answers in the future.

 

Happy Hacking!

 

Cheers,

Heath

LOL: Can you believe the thread author posted his message and then emailed me using the share.ez.no user content system to email me a request to come answer his question fairly shortly after it being posted. It was funny, not many people are quite so 'bold', no one has ever quite done that before, I should be honored? Still as Eek the Cat always used to say, "It never hurts to help" (right before the Eek the Cat would be mauled by Sharky the 'shark dog') in the Animated Television Show called Eak the Cat. Brings up fond memories...

Modified on Friday 06 January 2012 5:27:18 am by // Heath

Thursday 05 January 2012 6:16:23 pm

Hey Trash!

you can fetch objects nearly in the same way like you fetch them in your templates. Last time I did it this link explained it completely:
http://serwatka.net/blog/fetching_ez_publish_content_objects_with_php

As you will see it is more or less equal blunk.gif Emoticon But if you have still any doubts post them here.

Also the class-reference could be handy-tool for you:

http://pubsvn.ez.no/doxygen/4.0/html/classeZContentObject.html

Maybe its not the correct class i've chosen but you probably its the right way.

So, for now thats it, if you need more information or assistance post your comment.

Hope I could help you.

Best Regards

Chris

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from