Share » Forums » Developer » Create new objects/nodes through PHP

Create new objects/nodes through PHP

Create new objects/nodes through PHP

Tuesday 04 May 2010 4:19:21 am - 3 replies

Author Message

Håkan Bergman

Wednesday 05 May 2010 12:30:21 am

I tried a different code that will createandpublish:

$Params = array ();
$Params['parent_node_id'] = 92;
$Params['class_identifier'] = 'local_organisation';
$Params['creator_id'] = 14;
$Params['section_id'] = 2;

$AttributesData = array();
$AttributesData['name'] = 'My Company';
$Params['attributes'] = $attributesData;

$ContentObject = eZContentFunctions::createAndPublishObject($Params);

local_organisation is a custom class with one datatype with an identifier called "name".
The attribute have a default identifier "My Company". I have tried to manipulate the attribute data but nothing happens.
Is this a good way to create and publish an object below a parentnode?
Should I do this differently and does anyone know how I can manipulate the datatype data before it submits into the DB?
The code works very nice, but I am concerned if this is done the right way and what I should think of. My main problem right now is how to manipulate data. I have watched a few examples, but nothing works so far when trying to change the name of the attribute "name" from the default value I put in the class.

Best regards,

Håkan

Mark Simon

Wednesday 05 May 2010 1:30:07 am

This looks quite goot, but try to publish the new created object.

When creatind an Object and just storing it, You just have a draft of it.

after $nodeAssignment->store(); instert:

$contentObject->store();
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ), 'version' => 1 ) );

www.all2e.com

Paul Leclercq

Wednesday 05 May 2010 7:38:12 am

There are 2 good ways of publishing new content when creating an import script:

The one you stated in your second post:

exemple:

<span>$params</span> = <a href="http://www.php.net/array" mce_href="http://www.php.net/array"><span>array</span></a><span>(</span><span>)</span>;
<span>$params</span><span>[</span><span>'parent_node_id'</span><span>]</span> = <span>52</span>; <span>// node id of /Media/Files</span>
<span>$params</span><span>[</span><span>'class_identifier'</span><span>]</span> = <span>'file'</span>;
<span>$params</span><span>[</span><span>'creator_id'</span><span>]</span> = <span>14</span>; <span>// admin</span>
<span>$params</span><span>[</span><span>'storage_dir'</span><span>]</span> = <span>'/tmp/data/'</span>; <span>// don't forget the ended /</span>
<span>$params</span><span>[</span><span>'section_id'</span><span>]</span> = <span>3</span>; <span>// section media</span>
 
<span>$attributesData</span> = <a href="http://www.php.net/array" mce_href="http://www.php.net/array"><span>array</span></a><span>(</span><span>)</span>;
<span>$attributesData</span><span>[</span><span>'name'</span><span>]</span> = <span>'My file'</span>;
<span>$attributesData</span><span>[</span><span>'file'</span><span>]</span> = <span>'my_file.txt'</span>;
 
<span>$params</span><span>[</span><span>'attributes'</span><span>]</span> = <span>$attributesData</span>;
<span>$contentObject</span> = eZContentFunctions::<span>createAndPublishObject</span><span>(</span> <span>$params</span> <span>)</span>;

But if I were you, I would base my import script on the following file you will find in your ezpublish installation under:

bin/php/ezcsvimport.php

You will find Marks method in that file, which is the standard method used, and everything you need to create setup your attributes with the value you would like.

All you will need is to setup your $nodeID value.

This is also the code they give you during eZSystem's advanced ezpublish training course:

-----------------------------------

$class = eZContentClass::fetchByIdentifier( 'article' );
$contentObject = $class->instantiate( $creator );
$contentObject->store();

$nodeAssignment = eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $nodeID,
'is_main' => 1
)
);
$nodeAssignment->store();

$version = $contentObject->version( 1 );
$version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();

$attributes = $contentObject->attribute( 'contentobject_attributes' );
while ( list( $key, $attribute ) = each( $attributes ) )
{
$dataString = $objectData[$key];
switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
{
case 'ezimage':
case 'ezbinaryfile':
case 'ezmedia':
{
$dataString = eZDir::path( array( $storageDir, $dataString ) );
break;
}
default:
}
$attribute->fromString( $dataString );
$attribute->store();
}

$operationResult = eZOperationHandler::execute( 'content', 'publish',
array( 'object_id' => $contentObjectID,
'version'=> 1 ) );

-----------------------------------

Well good luck with that anyway,

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu