Twiddling bits for over 30 years

Home / Blogs / eZ Blog / Finding the objects of a class with non-null values

Finding the objects of a class with non-null values

For a customer i had to find the objects (out of hundreds) that had an attribute filled out, I figured an attribute filter would be the easiest:

 <?php
 
 
require 'autoload.php';
$cli = eZCLI::instance();
$script = eZScript::instance( array(    'description' => (
                                "Returns the path and value of objects of <class> that have a non-null value for <attribute>" ),
                                'use-session' => false,
                                'use-modules' => true,
                                'use-extensions' => true ) );
$script->startup();
 
 $options = $script->getOptions( "[class:][attribute:][C:][a:]",
                                                         "",
                                                       array( 'class'  => "class",
                                                                  'attribute'  => "attribute"
                                                      ) );
$script->initialize();
 
$identifier = $options['class'] ? $options['class'] : $options['C'];
$attidentifier = $options['attribute'] ? $options['attribute'] : $options['a'];
 
if (!$identifier OR !$attidentifier) {
    $cli->error( "You must have one --class=<class> identifier and one --attribute=<attribute> identifier");
    $script->shutdown();
    exit;
} 
 
if ( is_numeric( $identifier ) ) {
    $ContentObjectClass = eZContentClass::fetch( $identifier );
    $identifier = $ContentObjectClass->attribute('identifier');
}
 
if ($attidentifier) {
    if ( is_numeric( $attidentifier ) ) {
        $attidentifier =  eZContentClassAttribute::classAttributeIdentifierByID( $attidentifier );
    }
 
    $attributefilterstring = $identifier."/".$attidentifier;
    $attributefilter = array( array( $attributefilterstring ,'!=', null ) );
}
 
$params = array();
 
$params['Limitation']    = array();        // access override
$params['MainNodeOnly']    = true;    // dont fecth dupes
$params['IgnoreVisibility'] = true;       // ignore hidden
if ($identifier) {
    $params['ClassFilterType'] = "include";        // "include" or "exclude"
    $params['ClassFilterArray'] = array($identifier);    // array of class_identifiers
}
$params['AttributeFilter']    = ($attributefilter) ? $attributefilter : false;
 
$topNodeID = 1;
$objects = eZContentObjectTreeNode::subTreeByNodeID( $params, $topNodeID );
 
if ( count($objects) == 0 ) $cli->output( "NO MATCHES" );
else {
    $cli->output( count($objects) );
 
    foreach($objects as $object) {
        $cli->output( $object->attribute( 'path_identification_string') );
        $datamap = $object->DataMap();
        $cli->output( $datamap[$attidentifier]->content() );
    }
}
$cli->output();
$script->shutdown();
?>