Forums / Developer / Creating a Simple Content Object via PHP

Creating a Simple Content Object via PHP

Author Message

Dave Dykshoorn

Friday 19 September 2003 4:12:29 pm

I am looking for a simple guide to creating content objects via php. I have looked and found instructions on editing content, deleting content, and creating content classes however creating content objects seems to have been missed at the present.

All I really want to know is how to create a content object from a content class with 1 Text Line. That should be darn easy if I could find some reference. If anyone could help me out with a few lines of code it would be greatly appreciated.

Thanks in advance,

Dave

Willie Seabrook

Saturday 20 September 2003 5:46:57 am

Have you made any progress dave? I'm trying to accomplish this sort of thing also.

I have to import the data of about 5000 users into ezpublish system. Are there any docs on importing data? As well as the users problem I also have other data that needs to be turned into content objects.

Ez system people: help?

Regards,
Willie

Willie Seabrook

Monday 22 September 2003 3:28:36 am

Still looking around for a solution to this.... I thought I had found one in the sdk docs though... right at the bottom of the page at http://ez.no/sdk/kernel/view/content_objects

However, one can see that the author was interrupted to more important things :-)

Regards,
Willie

Willie Seabrook

Monday 22 September 2003 3:51:32 am

I've tried to scramble an approximate attempt together... many questions though: Want to help me along Dave? Have *you* made any progress?

Come on EZ!!!! One of you could demonstrate this is 5 minutes where it will take us a week to figure out the whole content tree model etc on our own!!!! :-( :-(

Regards,
Willie

###############################

$contentClassId = 3; //For ezUser, we would make this the ezUser class id but otherwise just pick and choose.
$name = "Blah?"; //What should this be?
$userId = 1; //The userId creating it. Since we are running this outside of the web for mainainence purposes its fine to use root.

$contentObject obj = ezContentObject::create($name, $contentClassId, $userId);

for each attribute that the content class has
$obj->setAttribute("name", "value"); //What if its a binary file or something like that?

$contentObject->store();

I don't see any way to place the new object in folders etc

$newUser = ezUser::create($contentObjectId);
$newUser->store();

#################################3

Wenyue Yu

Monday 22 September 2003 4:26:58 am

Hi,

I made a simple example according to Willie's code:

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

$contentClassId = 3; //For ezUser, we would make this the ezUser
//class id but otherwise just pick and choose.

//fetch this class
$class =& eZContentClass::fetch( 3 );

$userID = 1; //The userId creating it. Since we are
//running this outside of the web for mainainence purposes its fine to use root.

// Instantiate the object with user $userID and put it in section 1.
$contentObject =& $class->instantiate( $userID, 1 );

$name = "Blah?"; //What should this be?
//Answer: Object name

$contentObject->setAttribute( 'name', $name );

$parentNodeID = ? // choose where to put the user

$nodeAssignment =& eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $parentNodeID,
'sort_field' => 2,
'sort_order' => 0,
'is_main' => 1
)
);
$nodeAssignment->store();

$version =& $contentObject->version( 1 );

$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

$contentObjectID = $contentObject->attribute( 'id' );
$contentObjectAttributes =& $version->contentObjectAttributes();

// Set attribute valuse for this class depending on attributes it has and its attribute datatype
// Should be setAttribute( 'data_int', $attributeValue ) if the attribute is integer.
// If user, image, binary file, you also need to stort something in extra tables.
//You need to know how does
// eZ publish store these kind of content.
$contentObjectAttributes[0]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[0]->store();

$contentObjectAttributes[1]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[1]->store();

...

$contentObject->store();

// Publish it
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
'version' => 1 ) );

Dave Dykshoorn

Monday 22 September 2003 8:26:50 am

Hey thanks for helping me out Willie and eZp Crew. Sorry I couldn't contribute that much. I posted on my way out the door friday and just got in today.

I appreciate the code example and am about to put it into place. I'll let you know how the results go.

Again thanks

Dave

Dave Dykshoorn

Monday 22 September 2003 9:01:35 am

wenyue, (or whoever else knows), you mentioned that storing binary files need to store information in other tables. Can you expand on this a little. For instance say I have a file called test.pdf that I want to store with my content. Its located in C:\temp (or even better, as a blob in another mysql db table) how would I do this, using your sample code?

$contentObjectAttributes[0]->setAttribute( 'binaryfile', $attributeValue );
$contentObjectAttributes[0]->store();

Thanks in advance,

Dave

Dave Dykshoorn

Monday 22 September 2003 2:28:42 pm

I'm able to store content now of common int and text types however can you (or anyone else for that matter) explain how to store an object relation list and a binary file attribute with a new content object?

Basically I'm looking for a replacement for this, which i know is completely bogus:

$attributeValue = $file;
$contentObjectAttributes[1]->setAttribute( 'binary_file', $attributeValue );
$contentObjectAttributes[1]->store();

Wenyue Yu

Tuesday 23 September 2003 5:35:52 am

Hi,

It is a little bit complex to store binary file. It may work using following code:

$contentObjectAttribute =& $contentObjectAttributes[1]; // binary attribute

// $fileFileName and $fileOriginalFileName
// could be the same, for example "test.pdf"
saveFile( $fileFileName, $fileOriginalFileName, $contentObjectAttribute );
$contentObjectAttributes[1]->store();

// First, you need to put all files you want to
//upload under folder var/storage/files/ before you
//use this function
function saveFile( $fileFileName, $fileOriginalFileName, &$contentObjectAttribute )
{
include_once( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
include_once( "lib/ezutils/classes/ezmimetype.php" );
include_once( "lib/ezutils/classes/ezdir.php" );
$contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
$version = $contentObjectAttribute->attribute( "version" );

$file =& eZBinaryFile::create( $contentObjectAttributeID , $version );

$file->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
$file->setAttribute( "version", $version );
$file->setAttribute( "filename", $fileFileName );
$file->setAttribute( "original_filename", $fileOriginalFileName );

$mimeObj = new eZMimeType();
$mime = $mimeObj->mimeTypeFor( false, $fileOriginalFileName );
$file->setAttribute( "mime_type", $mime );
$file->store();

$sys =& eZSys::instance();
$storage_dir = $sys->storageDirectory();

list( $subdir, $extension )= split ("/", $mime );

$file_dir = $storage_dir . '/' . "original/" . $subdir;

if ( !file_exists( $file_dir ) )
{
eZDir::mkdir( $file_dir, 0777, true);
}

$source_file = $storage_dir . "/files/$fileFileName";
$target_file = $storage_dir . "/original/" . $subdir . "/$fileFileName";
copy($source_file, $target_file );
}

Dave Dykshoorn

Tuesday 23 September 2003 3:08:29 pm

Thank you for your help wenyue, I will try that out in the next couple days and let you know the results.

Dave

Dave Dykshoorn

Thursday 25 September 2003 8:58:41 am

Its official: at 8:30pm PST I successfully saved new content which includes searchable PDF's with code!

Thanks Wenyue for your help!

Dave

Willie Seabrook

Thursday 25 September 2003 5:42:21 pm

Hey Dave,

I was at the same stage as you on wednesday but have since had to spend my time fixing sendmail (Blah!!!) Would you be so kind as to email the code you used to me?? It would be very much appreciated!

willie [dot] seabrook [at] levity.co.nz

Regards,
Willie Seabrook

Claus Jensen

Wednesday 08 October 2003 3:17:05 am

Hello folks,
would it be possible to have a look at the code you produced to achieve this. It would be very much appreciated. If you put it out as a comment here or sent me an email: cuj[att]norgit[dot]no. Thanks in advance:)

And by the way, thanks to ez for sometimes sharing some of that information;)

cheers,
claÜs

Claus Jensen

Wednesday 08 October 2003 4:43:22 am

Hello again!
It seems to be working even vith some errors, but....
after trying out this code to import some content objects (using version 3.2-1), I get an error when I try to remove these objects. It says:

Fatal error: Undefined class name 'ezurlalias' in /data/ezpublish/3.2-1_Metadata/kernel/classes/ezcontentobjecttreenode.php on line 1902
Fatal error: eZ publish did not finish it's request

And some debug says:
Warning: PHP
Oct 08 2003 14:54:23
Undefined index: real_translation in /data/ezpublish/3.2-1_Metadata/kernel/classes/ezcontentobjecttreenode.php on line 1990

Did any of you encounter this? Here is my code, its partly from wenyue yu's script and another import.php script I found:
#!/usr/bin/php
<?php
set_time_limit( 0 );
//print( "Starting metadata import\n" );
include_once( "lib/ezutils/classes/ezdebug.php" );
//eZDebug::setHandleType( EZ_HANDLE_FROM_PHP );
include_once( "lib/ezutils/classes/ezmodule.php" );
eZModule::setGlobalPathList( array( "kernel" ) );
include_once( 'lib/ezutils/classes/ezexecution.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
eZDebug::setHandleType( EZ_HANDLE_TO_PHP );
eZDebug::setLogFileEnabled( false );
eZINI::setIsCacheEnabled( false );
function eZDBCleanup()
{
if ( class_exists( 'ezdb' )
and eZDB::hasInstance() )
{
$db =& eZDB::instance();
$db->setIsSQLOutputEnabled( false );
}
// session_write_close();
}
function eZFatalError()
{
eZDebug::setHandleType( EZ_HANDLE_NONE );
print( "Fatal error: eZ publish did not finish it's request\n" );
print( "The execution of eZ publish was abruptly ended." );
}
eZExecution::addCleanupHandler( 'eZDBCleanup' );
eZExecution::addFatalErrorHandler( 'eZFatalError' );
$db =& eZDB::instance();
$db->setIsSQLOutputEnabled( false );

$contentClassId = 28; //For metadata bruk 28

//fetch this class
$class =& eZContentClass::fetch( $contentClassId );

$userID = 1; //The userId creating it. Root = 1

// Instantiate the object with user $userID and put it in section 1.
$contentObject =& $class->instantiate( $userID, 1 );

$name = "Blah? blablablablabla"; //What should this be?
//Answer: Object name

$contentObject->setAttribute( 'name', $name );

$parentNodeID = 164; // choose where to put the object

$nodeAssignment =& eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $parentNodeID,
'sort_field' => 2,
'sort_order' => 0,
'is_main' => 1
)
);
$nodeAssignment->store();

$version =& $contentObject->version( 1 );

$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

$contentObjectID = $contentObject->attribute( 'id' );
$contentObjectAttributes =& $version->contentObjectAttributes();

// Set attribute values for this class depending on attributes it has and its attribute datatype
// Should be setAttribute( 'data_int', $attributeValue ) if the attribute is integer.
// If user, image, binary file, you also need to stort something in extra tables.
//You need to know how does
// eZ publish store these kind of content.

$attributeValue = 'Testing testing...claus';

$contentObjectAttributes[0]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[0]->store();

//$contentObjectAttributes[1]->setAttribute( 'data_text', $attributeValue );
//$contentObjectAttributes[1]->store();

//$contentObjectAttributes[2]->setAttribute( 'data_text', $attributeValue );
//$contentObjectAttributes[2]->store();

//$contentObjectAttributes[3]->setAttribute( 'data_text', $attributeValue );
//$contentObjectAttributes[3]->store();

$contentObjectAttributes[4]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[4]->store();

$contentObjectAttributes[5]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[5]->store();

$contentObjectAttributes[6]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[6]->store();

$contentObject->store();

// Publish it
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
'version' => 1 ) );

?>

Cheers,
claÜs

Ulitsa Tal Arik

Friday 02 December 2005 2:45:43 am

another type of problem :)
i have some code to create+save attribute+upload file...
almost everything work (everything work its mean - name, description stored, file saved in storage) , but file attribute didn't saved
(myme_type, size, etc)

function saveFile( $fileFileName, $fileOriginalFileName, &$contentObjectAttribute )
{
include_once( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
include_once( "lib/ezutils/classes/ezmimetype.php" );
include_once( "lib/ezutils/classes/ezdir.php" );
$contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
$version = $contentObjectAttribute->attribute( "version" );

// Create a new mime object.
$mimeObj = new eZMimeType();
$mime = $mimeObj->mimeTypeFor( false, strtolower( $fileOriginalFileName ) );

list( $subdir, $extension )= split ("/", $mime );

$file =& eZBinaryFile::create( $contentObjectAttributeID , $version );
$file->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
$file->setAttribute( "version", $version );
$file->setAttribute( "filename", $fileFileName );
$file->setAttribute( "original_filename", $fileFileName );
$file->setAttribute( "mime_type", $mime );	

$file->store();

$sys =& eZSys::instance();
$storage_dir = $sys->storageDirectory();

$file_dir = $storage_dir . '/' . "original/" . $subdir;
if ( !file_exists( $file_dir ) )
{
eZDir::mkdir( $file_dir, 0777, true);
}

$source_file = "c:/temp/".$fileFileName;
$target_file = $storage_dir . "/original/" . $subdir . "/$fileFileName";
copy($source_file, $target_file );
}

eZ debug

Timing: Jan 18 2025 19:10:30
Script start
Timing: Jan 18 2025 19:10:30
Module start 'content'
Timing: Jan 18 2025 19:10:32
Module end 'content'
Timing: Jan 18 2025 19:10:32
Script end

Main resources:

Total runtime1.9306 sec
Peak memory usage4,096.0000 KB
Database Queries236

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 587.8438180.8125
Module start 'content' 0.00611.7652 768.6563850.7344
Module end 'content' 1.77130.1592 1,619.3906361.3047
Script end 1.9306  1,980.6953 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00450.2328210.0002
Check MTime0.00160.0844210.0001
Mysql Total
Database connection0.00070.037610.0007
Mysqli_queries1.774191.89362360.0075
Looping result0.00260.13402340.0000
Template Total1.900598.420.9502
Template load0.00220.114120.0011
Template processing1.898398.323620.9491
Template load and register function0.00020.009910.0002
states
state_id_array0.00130.065710.0013
state_identifier_array0.00200.103520.0010
Override
Cache load0.00250.12912610.0000
Sytem overhead
Fetch class attribute can translate value0.00170.089260.0003
Fetch class attribute name0.00150.0751160.0001
XML
Image XML parsing0.00130.068660.0002
class_abstraction
Instantiating content class attribute0.00000.0013160.0000
General
dbfile0.00900.4665270.0003
String conversion0.00000.000230.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
15content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
62content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
50content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 131
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs