Forums / Install & configuration / How to create ezcontentobject programmatically

How to create ezcontentobject programmatically

Author Message

sergey podlesnyi

Thursday 27 February 2003 3:13:04 am

Imagine I have done "custom class" tutorial and have created a class "Car". How do I create objects of this class using PHP? I need to import data from old database into ez publish. Spent a week digging in PHP code but no result.

Could anybody post a sample code - how to create an instance of custom class, populate attributes and store into persistent storage.

Thank you!

Ulitsa Tal Arik

Wednesday 23 November 2005 10:39:29 pm

Hi, Sergey, do find solution how create objects using PHP? If you find it plz can you send it me :) koban@inbox.ru (syudya po date dumayu uzhe nashel... :) )

Bruce Morrison

Wednesday 23 November 2005 11:12:49 pm

Hi Guys

Have a look at http://ez.no/community/contribs/import_export/import_xml_data
If this doesn't help the are a number of contribs dealig with import (and export) at http://ez.no/community/contribs/import_export

HTH

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Ulitsa Tal Arik

Thursday 24 November 2005 1:33:02 am

Thanks, Bruce, but this import/export modules is very complicated for me (i just beginner), so i'll be very happy if you'll post just sample how to create content class with one attribute :)

<b>Thank you!</b>

Alexandre Abric

Thursday 24 November 2005 1:55:16 am

Hi,

Here is some sample code to create a user. I don't remember where I took this from ...

<?php 

include_once( 'kernel/classes/ezcontentobject.php' ); 
include_once( 'lib/ezlocale/classes/ezdatetime.php' ); 
  
// Testuser definition 
$firstName = "Hugo"; 
$lastName = "Heise"; 
$email = "hugo.heise@hoppecke.com"; 
$login= "h-waldmann"; 



echo "creating Account: ".$login." - ".$firstName." ".$lastName." / ".$email."<br>"; 

$userClassID = 4; 
$class =& eZContentClass::fetch( $userClassID ); 

$creatorID = 426; // 426 = N.Leutner 
$groupNodeID = 140; 

$contentObject =& $class->instantiate( $creatorID, 1 ); 
  
// Insert into Table eznode_assignment 
$nodeAssignment =& eZNodeAssignment::create( array( 
                                                 'contentobject_id' => $contentObject->attribute( 'id' ), 
                                                 'contentobject_version' => $contentObject->attribute( 'current_version' ),

                                                 'parent_node' => $groupNodeID, 
                                                 'sort_field' => 9, 
                                                 'is_main' => 1 
                                                 ) 
                                             ); 
$nodeAssignment->store(); 
  

// Insert into Table ezcontentobject_version  
$version =& $contentObject->version( 1 ); 
$version->setAttribute( 'modified', eZDateTime::currentTimeStamp() ); 
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT ); 
$version->store(); 



// Inserts into Table ezcontentobject_attribute 

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

    // Attribute2 = firstname 
    $contentObjectAttributes[0]->setAttribute( 'data_text', $firstName ); 
    $contentObjectAttributes[0]->store(); 

    // Attribute3 = lastname 
    $contentObjectAttributes[1]->setAttribute( 'data_text', $lastName ); 
    $contentObjectAttributes[1]->store(); 



// Inserts into Table ezuser 

    // Create user by parent attribute ID 
    $user =& eZUser::create( $contentObjectID ); 

    // Create password with 8 Charakters 
    $password = eZUser::createPassword( 8, $firstName . $lastName . $email ); 
    echo "Password lautet: ".$password."<br>"; 
    
    // Create passwordhash 
    $hash = $user->createHash( $login, $password, eZUser::site(), eZUser::hashType() ); 

    // Set attributes 
    $user->setAttribute('login', $login ); 
    $user->setAttribute('email', $email); 
    $user->setAttribute('password_hash', $hash ); 
    $user->setAttribute('password_hash_type', 2 ); 
    $user->store(); 

// Publish user 
// This function returns the error: Cannot execute operation 'publish' in module 'content', 
// no valid dataeZOperationHandler::execute

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

?> 

Alexandre Abric

Thursday 24 November 2005 1:57:38 am

And another sample code to create a folder (the method to populate the attributes is better in the previous code)

<?

function createFolderObject($user, $sectionID, $userNodeID=2, $folderTitle="No title")
{
  $folderClassID=1;
  
  $class =& eZContentClass::fetch( $folderClassID );
  $folderContentObject =& $class->instantiate( $user->id(), $sectionID );
  
  // Create a node for the object in the tree.
  $nodeAssignment =& eZNodeAssignment::create( array(
                         'contentobject_id' => $folderContentObject->attribute( 'id' ),
                         'contentobject_version' => 1,
                         'parent_node' => $userNodeID,
                         'sort_field' => 2, //Published
                         'sort_order' => 1, //Descending
                         'is_main' => 1));
  $nodeAssignment->store();
    
  // Set a status for the content object version
  $folderContentObjectVersion =& $folderContentObject->version($folderContentObject->attribute( 'current_version' ) );
  $folderContentObjectVersion->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT);
  $folderContentObjectVersion->store();

  // Set the title of the folder.
  $folderContentObjectAttributes =& $folderContentObjectVersion->contentObjectAttributes();
  foreach ($folderContentObjectAttributes as $folderAttribute)
  {
    // Each attribute has an attribute called 'identifier' that identifies it.    

    if ($folderAttribute->attribute("contentclass_attribute_identifier") == "title")
    {
      $folderAttribute->setAttribute("data_text",$folderTitle);
      $folderAttribute->store();
    }
  }
      
  // Now publish the object.
  $operationResult = eZOperationHandler::execute( 'content', 'publish',
                          array( 'object_id' => $folderContentObject->attribute( 'id' ),
                                 'version' => $folderContentObject->attribute('current_version' ) ) );
  
  return;
}

?>

Ulitsa Tal Arik

Thursday 24 November 2005 2:14:55 am

I have some code before... but i have one error too:
Fatal error: Call to a member function on a non-object in C:\ezpublish\ezpublish\bin\php\test.php on line 22

<b>line 22:</b> <i>$contentObject =& $class->instantiate( $creatorID, 1 );</i>

what it's mean?

Ulitsa Tal Arik

Thursday 24 November 2005 4:22:07 am

Sorry Guys!!!

the bug was in another place->
when i call:$class =& eZContentClass::fetch( $contentClassID );

fetch does not return object...
this bug was closed and work in ez here
( http://ez.no/community/bugs/ezcontentclass_fetch_does_not_return_an_object_for_certain_classes )

the problem was db connection.
EVERY THING OK! I was trying execute this php-file from shell and nothing work... now i try execute it like cron and everything is working perfectly!!!

Alexandre Abric

Thursday 24 November 2005 4:36:08 am

You must use this code in eZ Publish context, either in a cronjob or in a custom module. You should have a look at the doc ;-)

eZ debug

Timing: Jan 30 2025 19:36:01
Script start
Timing: Jan 30 2025 19:36:01
Module start 'content'
Timing: Jan 30 2025 19:36:01
Module end 'content'
Timing: Jan 30 2025 19:36:01
Script end

Main resources:

Total runtime0.2430 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0079 588.1797370.2656
Module start 'content' 0.00790.0138 958.44531,013.7500
Module end 'content' 0.02160.2213 1,972.19533,905.7891
Script end 0.2429  5,877.9844 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00471.9214200.0002
Check MTime0.00140.5763200.0001
Mysql Total
Database connection0.00070.277010.0007
Mysqli_queries0.130753.77571410.0009
Looping result0.00170.71131390.0000
Template Total0.220890.910.2208
Template load0.00100.392610.0010
Template processing0.219990.499910.2199
Override
Cache load0.00070.280610.0007
Sytem overhead
Fetch class attribute can translate value0.00281.152010.0028
XML
Image XML parsing0.00030.114410.0003
General
dbfile0.01285.2515200.0006
String conversion0.00000.002830.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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs