Thursday 18 November 2010 9:11:37 am
First we need create the file extension/ezpotutorial/bin/php/test.php
Code :
<?php set_time_limit ( 0 ); require 'autoload.php'; $cli = eZCLI::instance(); $script = eZScript::instance( array( 'description' => ( "eZPersistentObject tutorial.\n\n"), 'use-modules' => true, 'use-extensions' => true) ); $script->startup(); $script->initialize(); // Code Goes Here $script->shutdown(); ?>
Then we need to run the following commands in the command line from the root folder of our site:
$> php bin/php/ezcache.php --clear-all --purge $> php bin/php/ezpgenerateautoloads.php –e -p
To run the script you need to run the following command:
$> php extension/ezpotutorial/bin/php/test.php
The explanation of the eZScript API is out of the scope here, for more information there’s an article from eZPedia about command line scripts.
To create an object, you need to pass an array as parameter, this array contains associative values according to the table field, then to store the object you just need to call the store() function and the object information will be stored in the database table.
Code :
$simpleObj = TutorialFriendshipObject::create( array( 'user1_id' => 1, 'user2_id' => 3 )); $simpleObj->store();
Copy and paste the script above in our test.php where you below the “// Code Goes Here”, then run your script.
Your database should have a new row:
user1_id | user2_id | status |
1 | 3 | 0 |
Code :
$cond = array( 'user1_id' => 1, 'user2_id' => 3); $simpleObj = eZPersistentObject::fetchObject( TutorialFriendshipObject::definition(), null, $cond ); $cli->output( $simpleObj->attribute( 'status' ) );
The output should be ‘0’.
Code :
$cond = array( 'user1_id' => 1, 'user2_id' => 3); $simpleObj = eZPersistentObject::fetchObject( TutorialFriendshipObject::definition(), null, $cond ); $simpleObj->setAttribute( 'status',1 ); $simpleObj->store(); $cli->output( $simpleObj->attribute( 'status' ) );
In this sample we just set the status attribute value to '1' and stored it in the database. If we set the user1_id or user2_id attribute value it will be created a new row in the database table, because that attribute is part of the primary key.
Code :
$cond = array( 'user1_id' => 1, 'user2_id' => 3); eZPersistentObject::removeObject( TutorialFriendshipObject::definition(), $cond );
Or:
Code :
$cond=array( 'user1_id' => 1, 'user2_id' => 3); $simpleObj = eZPersistentObject::fetchObject( TutorialFriendshipObject::definition(), null, $cond ); $simpleObj->remove();
See the Appendix for a short explanation of transactions, and how to use them from the eZ Publish API.
Code :
// Creates two rows TutorialFriendshipObject::create( array('user1_id' => 1, 'user2_id' => 3, 'status' => self::STATUS_PENDING ))->store(); TutorialFriendshipObject::create( array('user1_id' => 2, 'user2_id' => 4, 'status' => self::STATUS_PENDING ))->store(); $cond = array(); $list = eZPersistentObject::fetchObjectList( TutorialFriendshipObject::definition(), null, $cond ); $cli->output( "Listing objects:\n" ); foreach( $list as $obj ) { $cli->output( "Object Status: ". $obj->attribute('status') . "\n" ); }
The output should be:
Listing objects: Object Status: 0 Object Status: 0