Share » Learn » eZ Publish » A Quick and Friendly Introduction to...

A Quick and Friendly Introduction to eZPersistentObject

Thursday 18 November 2010 9:11:37 am

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

Step 2: Creating our class

We start creating the file extension/ezpotutorial/classes/tutorialfriendshipobject.php:

Code :

<?php

class TutorialFriendshipObject extends eZPersistentObject
{
     const STATUS_ACCEPTED = 1;
     const STATUS_PENDING = 0;
     /**
     * Construct, use {@link UserExpObject::create()} to create new objects.
     * 
     * @param array $row
     */
    protected function __construct(  $row )
    {
        parent::eZPersistentObject( $row );
    }

    public static function definition()
    {
        static $def = array( 'fields' => array(
                    'user1_id' => array(
                                       'name' => 'user1_id',
                                       'datatype' => 'integer',
                                       'default' => 0,
                                       'required' => true ),
                    'user2_id' => array(
                                       'name' => 'user2_id',
                                       'datatype' => 'integer',
                                       'default' => 0,
                                       'required' => true ),
                    'status' => array(
                                       'name' => 'status',
                                       'datatype' => 'integer',
                                       'default' => self::STATUS_PENDING,
                                       'required' => true ),
                  ),
                  'keys' => array( 'user1_id', 'user2_id' ),
                  'class_name' => 'TutorialFriendshipObject',
                  'name' => 'tutorial_friendship' );
        return $def;
    }

    public static function create( array $row = array() )
    {    
        if( $row['status'] != self::STATUS_ACCEPTED 
                    and $row['status']!= self::STATUS_PENDING )
        {
            $row['status']= self::STATUS_PENDING;
        }
        $object = new self( $row );
        return $object;
    }
    
}
?>
 

In this example class, the constructor receives an associative array with the name and values of the object attributes ( user1_id, user2_id, and/or status ) and create the eZPersistentObject. The definition function returns an associative array that specify the object metadata by declaring its fields, keys, database table and so on. When creating a class that inherits from eZPersistentObject you need to implement this function. Here's a brief description of all the necessary information:

 
fields An associative array of fields which defines which database field (the key) is to fetched and how they map to object member variables (the value). The datatype field can be int, integer, float, double, or string.
keys An array of fields which is used for uniquely identifying the object in the table.
function_attributes An associative array of attributes which maps to member functions, used for fetching data with functions.
set_functions An associative array of attributes which maps to member functions, used for setting data with functions.
increment_key The field which is incremented on table inserts.
class_name The classname which is used for instantiating new objects when fetching from the database. The name of the class we are working on.
sort An associative array which defines the default sorting of lists, the key is the table field while the value is the sorting method which is either asc or desc. Caution with high volume of data, it can decrease the performance. The sort will be applied to every query when no explicit sort is demanded.
name The name of the database table
 
36 542 Users on board!

Tutorial menu

Printable

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

Author(s)