Solution: Adding related object programmatic

« 
Previous article
|
|

Question

How to add related object programmatically ...

Answer

To add a list of object as an object relation through CLI, this code is not perfect and may remove any existing object relations you have.

Example

´╗┐Here is an example eZ Publish PHP CLI Script which shows how to use this feature.

<?php
/**
 * tmp
 *
 * @package tmp
 * @author Olav Frengstad <olav.frengstad@07.no>
 */
require_once 'autoload.php';

$cli = eZCLI::instance();

$script = eZScript::instance( array(
        'description' => 'test script',
        'use-modules' => true,
        'use-extensions' => true,
) );

$script->startup();

$config = $argumentConfig = '';

$optionHelp = $arguments = false;
$useStandardOptions = array(
        'user'          => true,
        'siteaccess'    => true,
);
$options = $script->getOptions(
        $config,
        $argumentConfig,
        $optionHelp,
        $arguments,
        $useStandardOptions
);

$script->initialize();

$user = eZUser::fetchByName('admin');
if ( !$user )
{
        $user = eZUser::currentUser();
}

$rel = array(153);
$object = eZContentObject::fetch( 154 );

$object->appendInputRelationList( $rel,eZContentObject::RELATION_LINK );
$object->commitInputRelations($object->CurrentVersion);
$object->store();

$script->shutdown();

To run it save the file in your ez directory (preferably put it in an extension) and call it:

$ php relation.php --siteaccess <your-siteaccess-name>