Forums / Developer / PHP remove objects script ?

PHP remove objects script ?

Author Message

H-Works Agency

Saturday 14 July 2007 12:28:31 pm

Hi all,

I am using a cron task to run a php script to synchronize the ezpublish product database with my client real shop.

Part of this script is made to remove objects that are no longer available. For doing this i use :

eZContentObjectTreeNode::removeSubtrees($deleteIDArray, $moveToTrash=false);

Where deleteIDArray is an array holding all node_id that need removal.

The problem is that this function doesn't work. It doesn't remove the nodes.

Why that ? Is this the good process to remove my objects in a php script ?

EZP is Great

Kristof Coomans

Sunday 15 July 2007 5:10:41 am

Hello Martin

You probably need to log in as a user that haves the right policies to remove the nodes.

See http://ezpedia.org/wiki/en/ez/command_line_scripts for a generic script example. One option is to use

$useStandardOptions = array( 'user' => true );

Afterwards, you can specify login and password on the command line with --login and --password

If you want to run the script from cron, you're probably better off with the second option:

include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
$user = eZUser::fetchByName( 'admin' );
$userID = $user->attribute( 'contentobject_id' );
eZUser::setCurrentlyLoggedInUser( $user, $userID );

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

H-Works Agency

Monday 16 July 2007 5:08:26 am

Thanks - in fact it has to do with the rights management thru cronjobs.

I putted this code at the start of my script and it seems to work :

include_once('kernel/classes/datatypes/ezuser/ezuser.php');

$ini =& eZINI::instance();     
$userCreatorID = $ini->variable( "UserSettings", "UserCreatorID" );
$user = eZUser::fetch($userCreatorID);

if(!$user)
{
    $cli->error("Cannot get user object by userID = '$userCreatorID'. See site.ini[UserSettings].UserCreatorID");
    $script->shutdown(1);
}
                               
// Login
eZUser::setCurrentlyLoggedInUser($user, $userCreatorID);

EZP is Great