eZ Community » Forums » Developer » FetchByIdentifier
expandshrink

FetchByIdentifier

FetchByIdentifier

Friday 15 June 2012 12:23:51 pm - 3 replies

Hello,

I've created a content class, with a few attributes. One of the attributes is an identifier.

From the PHP code, I'm trying to retrieve a content object based on its identifier (as I have neither it's content object ID, nor its node ID).

 

 
 
     $objAttr = eZContentObjectAttribute::fetchByIdentifier($buf['identifier']);
 
         if ($objAttr !== null)
         {
             $myContentObject = eZContentObject::fetch($objAttr->ContentObjectID);

My problem is that when I have content objects in the trash with the same identifier as 'live' objects, the fetchByIdentifier retrieves these trashed objects.

 

How can I avoid that ? And is there a more efficient method to retrieve my content object with only one fetch ?

 

Thanks

David

Modified on Friday 15 June 2012 1:55:34 pm by H R

Friday 15 June 2012 2:31:43 pm

Hi,

you could use the same function which is used for content tree /list fetch in templates

 $NodeList = eZFunctionHandler::execute( 'content', 'tree', 
  array(
    'parent_node_id'     => 2,
    'limit'              => 500, 
    'class_filter_type'  => 'include',                                                                        
    'class_filter_array' => array( 'article' ),
     'depth'              => 5
  ) );

All paramaters you will find here

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

You have to add an attribute_filter to filter your objects of your class by attribute.

Hope this will help

Cheers Felix

Modified on Friday 15 June 2012 2:33:04 pm by Felix Woldt

Friday 15 June 2012 5:43:00 pm

Thanks Felix!

I've got the following piece of code, that seems to work. Is this not possible to accomplish this with only one fetch?

                     $NodeList = eZFunctionHandler::execute( 'content', 'tree', 
                    array(
                        'parent_node_id'     => 2,
                        'limit'              => 1, 
                        'class_filter_type'  => 'include',                                                                        
                        'class_filter_array' => array( 'mycontentclass' ),
                        'depth'              => 5,
                        'attribute_filter'   => array('and', array('mycontentclass/myidentifier', '=', $buf['identifier']))
                    ) );
 
                if(count($NodeList) >0)
                    {
                        $node = $NodeList[0];
                        $myContentObject = eZContentObject::fetch($node->ContentObjectID);
                    }

Thanks

David

Modified on Friday 15 June 2012 5:44:09 pm by H R

Monday 18 June 2012 11:24:17 am

Hello,

try this:

$myContentObject = $NodeList[0]->object();

Daisy

Modified on Monday 18 June 2012 11:25:12 am by Daisy Radix Leroy

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from