Forums / Developer / eZ API: removing an object (and all of its nodes)

eZ API: removing an object (and all of its nodes)

Author Message

Piotrek Karaś

Monday 05 May 2008 11:49:54 am

Hi there,

Could anyone provide me with a recipe for completely removing an object (all of its versions, nodes, references/relations, etc...) via eZ API, leaving the installation in a consistent state? I've spent some time reading the kernel, some things worked better, some worse, but nothing worked 100%, and I'm running out of time ;) Basically, I'm looking for an operation list to remove object (all of its stuff) by its object ID.

I'd be grateful for any help on this.
Piotrek

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Łukasz Serwatka

Tuesday 06 May 2008 12:04:25 am

Hi,

Take a look on eZContentObjectTreeNode::removeSubtrees( ) method. It takes array of nodeIDs by that you can easily find if you know objectID.

You can also use: bin\php\ezsubtreeremove.php from CLI.

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

Pascal von Büren

Tuesday 06 May 2008 1:54:48 am

Hi Piotrek,

this seems to do the job fairly well:

	function deleteObject($objectId)
	{

    $moveToTrash = false;
		$object = eZContentObject::fetch((int) $objectId);
		if (!is_object($object))
		{
			return false;
		}
		$nodes = $object->attribute('assigned_nodes');
		//remove all nodes that are not main and their subtree
		foreach ($nodes as $node)
		{
			if (!$node->attribute('is_main'))
			{
        eZContentObjectTreeNode::removeSubtrees( array($node->attribute('node_id')), $moveToTrash );
			}
		}
		//loop again for the mainnode
		$nodes = $object->attribute('assigned_nodes');
		foreach ($nodes as $node)
		{
      eZContentObjectTreeNode::removeSubtrees( array($node->attribute('node_id')), $moveToTrash );
		}

		return true;
	}

Piotrek Karaś

Tuesday 06 May 2008 2:55:09 am

Thanks guys. Seems like I've been barking up <b>the right</b> tree ;) It just went wrong somehow... well, at least I know that I have to dig deeper ;)

Cheers,
Piotrek

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

André R.

Tuesday 06 May 2008 5:06:12 am

Slightly slimmer:

function deleteObject( $objectId )
{
	$moveToTrash = false;
	$object = eZContentObject::fetch( (int) $objectId );

	if (!is_object($object))
	{
		return false;
	}

	$nodes = $object->attribute('assigned_nodes');
	$mainNodeId = $object->attribute('main_node_id');
	//remove all nodes that are not main and their subtree
	foreach ($nodes as $node)
	{
		if ( $node->attribute('node_id') != mainNodeId )
		{
			eZContentObjectTreeNode::removeSubtrees( array($node->attribute('node_id')), $moveToTrash );
		}
	}
        // remove main node an it's subtree
	eZContentObjectTreeNode::removeSubtrees( array( $mainNodeId ), $moveToTrash );
	return true;
}

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom