Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,059 of 9,059 pages
About the help in other languages
All help pages

eZPersistentObject

The eZPersistentObject reference - definition() keys, fetchObject(), fetchObjectList() and its parameters, count(), store(), remove(), removeObject(), attribute access - with kernel signatures.

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/help/extension-development/part-8-reference/ezpersistentobject.md

On this page

eZPersistentObject

Part 8, reference 5. eZPersistentObject (kernel/classes/ezpersistentobject.php) maps a table row to an object. The kernel's own classes (content objects, nodes, users, roles) are built on it, and it is the recommended way to keep your own tables. The tutorial is Persistent objects and your own tables and Your own table with a persistent object, by hand. This page is the reference.

definition()

A public static function definition() returns an array:

KeyMeaning
fieldscolumn name => array( 'name' => attribute name, 'datatype' => 'integer'|'string'|'float'|'text', 'default' => ..., 'required' => bool )
keysthe primary key columns
increment_keythe auto-increment column, if any
class_namethe class rows become
namethe table name
sortdefault sort, array( column => 'asc'|'desc' )
function_attributesattribute name => method name, for computed attributes readable through attribute() (and templates)
set_functionsattribute name => method name, called by setAttribute()

Fetching

public static function fetchObject( $def, $field_filters, $conds, $asObject = true, $grouping = null, $custom_fields = null )

public static function fetchObjectList( $def, $field_filters = null, $conds = [], $sorts = null, $limit = null,
                                        $asObject = true, $grouping = false, $custom_fields = null,
                                        $custom_tables = null, $custom_conds = null )

public static function count( $def, $conds = null, $field = null )
ParameterMeaning
$defyour definition()
$field_filtersthe columns to fetch; null for all
$condsarray( column => value ); a value may be array( '<', 10 ) (also >, <=, >=, <>, like), array( array( 1, 2, 3 ) ) for IN, or array( false, array( 200, 500 ) ) for BETWEEN
$sortsarray( column => 'asc' ); null uses the definition's sort
$limitarray( 'offset' => 0, 'limit' => 20 )
$asObjectfalse returns plain arrays
<?php
class myExtItem extends eZPersistentObject
{
    public static function definition()
    {
        return array(
            'fields' => array(
                'id' => array( 'name' => 'ID', 'datatype' => 'integer', 'default' => 0, 'required' => true ),
                'parent_id' => array( 'name' => 'ParentID', 'datatype' => 'integer', 'default' => 0, 'required' => true ),
                'name' => array( 'name' => 'Name', 'datatype' => 'string', 'default' => '', 'required' => true ),
            ),
            'keys' => array( 'id' ),
            'increment_key' => 'id',
            'class_name' => 'myExtItem',
            'sort' => array( 'name' => 'asc' ),
            'name' => 'myext_item',
        );
    }

    public static function fetch( $id )
    {
        return eZPersistentObject::fetchObject( self::definition(), null, array( 'id' => (int)$id ) );
    }

    public static function fetchByParent( $parentID, $offset = 0, $limit = 20 )
    {
        return eZPersistentObject::fetchObjectList( self::definition(), null, array( 'parent_id' => (int)$parentID ),
                                                    null, array( 'offset' => $offset, 'limit' => $limit ) );
    }

    public static function countByParent( $parentID )
    {
        return eZPersistentObject::count( self::definition(), array( 'parent_id' => (int)$parentID ) );
    }
}

fetchObject() returns null when nothing matches. Condition values are escaped and quoted by the class (as strings); still cast values from the request to the expected type.

Writing

MethodUse
new myExtItem( $row ) then store( $fieldFilters = null )insert, or update when the key exists; sets the increment key after an insert
setAttribute( $attr, $val )change a value (calls a set_functions method if defined)
remove( $conditions = null, $extraConditions = null )delete this row
removeObject( $def, $conditions = null, $extraConditions = null ) (static)delete matching rows
updateObjectList( $parameters ) (static)update matching rows without fetching them
sync( $fieldFilters = null )store only when there are changes
<?php
$item = new myExtItem( array( 'parent_id' => 2, 'name' => 'First' ) );
$item->store();
$id = $item->attribute( 'id' );

Reading attributes

MethodReturns
attribute( $attr, $noFunction = false )a field's value, or the result of a function_attributes method
hasAttribute( $attr )whether the attribute exists
attributes()all attribute names, including function attributes

These three methods make objects readable in templates: {$item.name}.

On every engine

eZPersistentObject builds the SQL for the configured engine, and on MongoDB installations the adapter turns its calls into MongoDB queries (MongoDB kernel support). It also keeps fields the class does not declare in an internal array, so PHP 8.2's dynamic property deprecation does not apply to subclasses (Dynamic properties).

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x