eZ Community » Forums » Developer » eZContentObjectTreeNode::fetch goes...
expandshrink

eZContentObjectTreeNode::fetch goes wrong

eZContentObjectTreeNode::fetch goes wrong

Wednesday 30 November 2011 2:38:23 pm - 6 replies

Hello, 

I'm trying to list all the elements located in a same node. 

I'm using, with PHP, the static method eZContentObjectTreeNode::fetch( $nodeID ).

In my example, $nodeID equals '99', the node id value.

The Back-office list says that there over 8 000 elements, but the function returns me an empty array.

Is there something that I didn't make correctly ?

Could you possibly help me ?

Thanks a lot

Wednesday 30 November 2011 2:52:26 pm

Hello Jerome,

You might get more traction posting a more complete example of the PHP source code your using (beyond this one rather basic api call).

Also which context is this code being run?

Have you checked your siteaccess settings, are you certain your truly using the same database in both contexts?

 

I hope this helps ...

 

Cheers,

Heath

Wednesday 30 November 2011 2:58:57 pm

Hey Jerome,

 

if u use $node=eZContentObjectTreeNode::fetch( $nodeID );

$node is a Object and no Array, u have to get your Data like this

$dataMap = $node->dataMap();

$name = $dataMap['name']->attribute('data_text');

Modified on Wednesday 30 November 2011 3:04:20 pm by scrieler _

Wednesday 30 November 2011 3:03:48 pm

Of course, with some information, it can be helpful !! Sorry.

We'll use the command line to execute the script, in order to store data in a searchEngine, each night.

Here is the sequence : 

require_once('autoload.php');
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'lib/ezutils/classes/ezextension.php' );
include_once( 'kernel/classes/ezscript.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );

$limit = 200;

$count = 0;

$node = eZContentObjectTreeNode::fetch( 99); //$node is always an empty array

if ($subTree = $node->subTree(array('Offset' => $offset, 'Limit' => $limit, 'Limitation' => array(), 'MainNodeOnly' => true))) {

  foreach ($subTree as $innerNode) {

     $object = $innerNode->attribute( 'object' );

    if ( !$object ) { continue; }

    $searchEngine->addObject( $object, false );

++$count;

  }   

}

echo 'Total -> '.$count;

Wednesday 30 November 2011 3:13:54 pm

Hello Jerome,

Sorry I ran out of time this morning to comment in more detail so I will be brief ...

But I did want to mention, not that it matters, but ... in eZ 4.x you no longer need the following code block and you can remove it entirely without question.

include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'lib/ezutils/classes/ezextension.php' );
include_once( 'kernel/classes/ezscript.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );

Also scrieler is right an $node when correctly fetched will -not- return an array (ever) it will return an object. Which by your code I see then uses it to fetch subtree children.

You may have a problem with your subtree fetch and not your node fetch, you may want to validate this in further detail.

Also worth noting you have a subtree fetch limit of '200' so you will never get the full results as long as this limit is in places (I suggest a loop of 200 items fetched each loop until complete instead, your use case may require different logic granted)

 

I hope this helps ...

Cheers,

Heath

Wednesday 30 November 2011 5:18:53 pm

Scripts are run under the Anonymous account, so your datas should be accessible by Anonymous : beware of sections for exemple.

Modified on Wednesday 30 November 2011 5:19:18 pm by Damien MARTIN

Wednesday 30 November 2011 7:57:34 pm

Hello Jerome,

If your content is within a restricted section you can do like this script to run the script as 'admin' user (full privs) instead of anonymous.

https://github.com/brookinsconsul...n/php/bcsubtreeremovebyrange.php#L92

Example

// Get user's ID who can remove subtrees. (Admin by default with userID = 14)
 
$userCreatorID = $ini->variable( "UserSettings", "UserCreatorID" );
$user = eZUser::fetch( $userCreatorID );
 
// Test for valid user object
if ( !$user ){
    $cli->error( "Subtree remove Error!\nCannot get user object by userID = '$userCreatorID'.\n(See site.ini[UserSettings].UserCreatorID)" );
    $script->shutdown( 1 );
}
 
// Login as user
eZUser::setCurrentlyLoggedInUser( $user, $userCreatorID );

I hope this helps ...

 

Cheers,

Heath

Modified on Wednesday 30 November 2011 7:59:26 pm by Heath Heath

expandshrink

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

36 542 Users on board!

Forums menu