This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Learn » eZ Publish » Fetching User Objects with PHP - part 2

Fetching User Objects with PHP - part 2

Monday 18 October 2010 10:38:28 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 1 : Filtering Users using subTreeByNodeID

We've looked at a few ways of pulling out multiple users but by far the most useful is achieved by using Fetch statements. We're going to be using the function eZContentObjectTreeNode::subTreeByNodeID() as this is extremely flexible and can be adapted to suit whatever content classes you need to export or access in your PHP.

Below is a fairly simple example using the function. It pulls out the first 5 users found on the site within Users/Members (we’ll break the script down once we’ve looked at it).

The example assumes you already seen the first part of the tutorial, this will give you a basic script you can house the rest of the code examples in.

$offset = 0; //Offset defaults to zero if omitted
$limit=5; //lets limit the fetch to 5 to prevent too many results from being returned
$depth = 1; //Depth defaults to one if it is excluded
$includeClasses = array( 'user' ); //please note this refers to content classes created in the CMS, rather than PHP files
 
$params = array( 'Depth' => $depth,
                 'Offset' => $offset,
                 'Limit' => $limit,
                 'ClassFilterType' => 'include',
                 'ClassFilterArray' => $includeClasses);
 
// getting the ID of where the users sit in the CMS (limiting the area eZ has to search for the objects):
$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'users/members' );
$parent_node_id = $parent_node->attribute( 'node_id' );
$results = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );
 

The fetch statement

There are a few areas to pay attention to in this example so we'll break it down and look at each area. We will start at the end since this is the function call itself. We will then look at what we are passing in to the function:

$results = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );

The code is equivalent to the following fetch statement (parameters are specified for completeness) :

{def $fetch_nodes=fetch( 'content', 'list', hash( 'parent_node_id', 2, 
                                                  'offset', 0, 
                                                  'limit', 10) )}

Unlike the fetch statement where a single associative array contains both the parent node ID and the other possible parameters, the PHP version separates the two. Our first parameter can contain filters, offsets and limits while our second parameter is solely the parent node ID.

 

The parameter array

$offset = 0; //Offset defaults to zero if omitted
$limit=5; //lets limit the fetch to 5 to prevent too many results from being returned
$depth = 1; //Depth defaults to one if it is excluded
$includeClasses = array( 'user' ); //please note this refers to content classes created in the CMS, rather than PHP files
 
$params = array( 'Depth' => $depth,
                 'Offset' => $offset,
                 'Limit' => $limit,
                 'ClassFilterType' => 'include',
                 'ClassFilterArray' => $includeClasses);

This section of the code is split into two parts. The first part is used to set the variables and the second just stores these in an array. Below is a complete list of the parameters you can pass into the system. They work in the same way as the parameters do in the fetch statement. Our following examples will include the other parameters so you should have a fully working example of what you need to export by the end of the tutorial.

 

Parameter

About

Example

Depth By default set to 1, this value tells eZ how many levels it should search down. array( ‘Depth’ => 3 )
Offset By default set to 0, this value is usually used in conjunction with Limit to control the offset of the results. array( ‘Offset’ => 20 )
Limit By default all of the results are returned. This can be set in conjuction with Offset to make your queries more resource friendly. array( ‘Limit’ => 20 )
SortBy This can be used to sort in order of name, published date or any attribute values. You must use true/false to specify ASC/DESC. array( ‘Limit’ => array( “name”, true ) )
AttributeFilter This is the key parameter which we will look into in more detail shortly. For user export you may use this in a number of ways, whether to check users who wish to be contacted or users created within a specified timespan. Our example here is simple. We will look at more complex examples but I would also recommend looking at the content list documentation on the eZ.no site for more examples. array( ‘AttributeFilter’ => array( array( ‘name’, ‘like’, ‘David*’ ) ))
ExtendedAttributeFilter This is a topic in itself so it is not covered here. Check out the examples in the fetch documentation if you need to use this with your custom queries.  
ClassFilterType / ClassFilterArray These are used in tandem so we will show their use together. ClassFilterType can be set to include or exclude. The classes filtered relate to the content types created in the CMS (such as User and Article) and do not relate to physical PHP file types (like eZUser). Please note that you need to use the class identifier rather than the class name. Array( ‘ClassFilterType’ => ’include’,
'ClassFilterArray’ => array( ‘user’ )
)
GroupBy Deprecated and not really that useful. Check out the codebase of eZContentObjectTreeNode if you want to use this.  
 

The Parent Node ID

$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'users/members' ); //note the lowercase. URLs use underscores rather than hyphens
$parent_node_id = $parent_node->attribute( 'node_id' );

There are a couple of points to mention here. Although we could easily hard code a value for the parent node id in our code I prefer not too for code readability and because there is less chance of making a mistake. Note the use of lowercase and also be wary that you change any hyphens to underscores (for example My-Content would become my_content).

Finally, be aware that a lot of our examples in part one of this tutorial have used the Content Object ID whereas here we need to use the Node ID.

 
36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from