Forums / Developer / Fetch objects having children with a particular attribute

Fetch objects having children with a particular attribute

Author Message

Andrey Astakhov

Monday 23 October 2006 12:14:28 pm

Could you give me an idea on how to fetch in a template a list of objects having children with a particular attribute matching given criteria?

I have got

Parent object 1
---- Child Object 1 (attribute1 = 'a')
---- Child Object 2 (attribute1 = 'c')

Parent object 2
---- Child Object 3 (attribute1 = 'b')
---- Child Object 4 (attribute1 = 'c')

Parent object 3
---- Child Object 4 (attribute1 = 'b')
---- Child Object 5 (attribute1 = 'a')

I need a list of parent objects that have attribute1 = 'a' in child objects.

How can i fetch this objects in my template? Thank you in advance.

Łukasz Serwatka

Tuesday 24 October 2006 12:14:28 am

Hi,

Use tree fetch with attribute_filter ( http://ez.no/doc/ez_publish/technical_manual/3_8/reference/modules/content/fetch_functions/tree ), then in loop display parent names.

{def $nodes=fetch( content, tree, ... )}

{foreach $nodes as $node }
    {$node.parent.name}
{/foreach}

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

Andrey Astakhov

Tuesday 24 October 2006 4:57:09 am

Thank you, Lukasz.
From my understanding in this case tree fetch will return child objects matching given criteria but not parent objects. Is it correct?

Łukasz Serwatka

Tuesday 24 October 2006 5:10:00 am

Yes, that is correct. You can also try with extended attribute filtering but that is more complex way. There is an example in doc.

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

Andrey Astakhov

Tuesday 24 October 2006 5:24:42 am

Extended attribute filter is still difficult for me. I'm trying to do it on template level.

Ok, if it's correct then it is not what i need.

How do you think which approach is better:

1) Get all child objects ($childs) filtered by a criteria attribute1 = 'a' . Then get a list of parent objects for this child objects $childs. Is it possible?

or

2) Get all parent objects ($parents). Then iterate through $parents getting childs for each item and filtering by criteria attribute1 = 'a'.

?