This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Learn » eZ Publish » Creating eZ Publish Objects in PHP

Creating eZ Publish Objects in PHP

Friday 04 June 2010 5:09:02 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 3 - Adding the Content (continued)

Adding related objects

Adding related objects to an attribute

Various types in eZ Publish uses a related object as an attribute (so that you can specify a file which can be attached to an article, for instance). To do this you just need to supply the object ID you just created as the attribute. The code below assumes you have just created an image using the code above. We are then going to use the object id of the image to attach the image to an image gallery.

Code :

//after the previous example store the object ID of the image:
$image_id = $imageObject->attribute( 'id' );
 
...
//then, when you specify the attributes of the image gallery use the ID: 
$attributesData['image'] = $image_id;

Although this approach will work in certain instances, there are other times it will not. For instance, The example of Image Galleries we have just used. The problem is that the image we will want to add to the gallery should be sitting below it. Since we have not even published the gallery yet we have no way at this point of attaching something which should sit underneath it.

The solution for this is to publish without the related object initially. There is a comprable method to the one we have been using in this tutorial for updating content and so when we have added all of the images as well, we can then call the update method instead. The update method is a static method called eZContentFunctions ::updateAndPublishObject. If you bring up the class definition within your IDE then there is an example of it in use (the file is located here: kernel/classes/ezcontentfunctions.php).

Adding related objects to an object

Since every node in eZ Publish can also have other related objects this is also a key thing you may have to do. It is not strictly possible with the createAndPublishObject function we are using but the function returns the content object that has been created. Due to this we can attach the related objects directly to this. The code below assumes we are pulling out the image id as we were previously and attach it to another node :

Code :

$image_id = $imageObject->attribute( 'id' ); //first we need the object id, let's pull out the image ID as we did before//create & publish the object we need the image to link to$contentObject->appendInputRelationList( array( $image_id ), eZContentObject::RELATION_COMMON );
//create the relation
$contentObject->commitInputRelations( $contentObject->CurrentVersion ); //commit to store it in the database

Adding Other Content

If you want to extend the script for further variable types then I would check out the FromString method documentation. This covers examples such as Date, matrices and keywords.

 

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from