Documentation

BaseMapper
in package
implements Countable, Mapper

Rdo Mapper Base class.

Feature parity with traditional Horde_Rdo

Controls mapping of entity objects (instances of Base) from and to Horde_Db_Adapters.

Public properties: $adapter - Horde_Db_Adapter that stores this Mapper's objects.

$inflector - The Horde_Support_Inflector this mapper uses to singularize and pluralize PHP class, database table, and database field/key names.

$table - The Horde_Db_Adapter_Base_TableDefinition object describing the main table of this entity.

Tags
category

Horde

Interfaces, Classes, Traits and Enums

Countable
Mapper

Table of Contents

$_classname  : string
What class should this Mapper create for objects? Defaults to the Mapper subclass' name minus "Mapper". So if the Rdo_Mapper subclass is UserMapper, it will default to trying to create User objects.
$_defaultSort  : string
Default sorting rule to use for all queries made with this mapper. This is a SQL ORDER BY fragment (without 'ORDER BY').
$_factory  : Factory
The caching factory, if used
$_lazyFields  : array<string|int, mixed>
Fields that should only be read from the database when they are accessed.
$_lazyRelationships  : array<string|int, mixed>
Relationships that should only be read from the database when they are accessed.
$_relationships  : array<string|int, mixed>
Relationships for this entity.
$_setTimestamps  : bool
If this is true and fields named created_at and updated_at are present, Rdo will automatically set creation and last updated timestamps.
$_tableDefinition  : Horde_Db_Adapter_Base_TableDefinition
The definition of the database table (or view, etc.) that holds this Mapper's objects.
__construct()  : mixed
__get()  : mixed
Provide read-only, on-demand access to several properties. This method will only be called for properties that aren't already present; once a property is fetched once it is cached and returned directly on any subsequent access.
addRelation()  : mixed
Adds a relation.
count()  : int
Count objects that match $query.
create()  : Base
Create a new object in the backend with $fields as initial values.
delete()  : int
Deletes a record from the backend. $object can be either a primary key, an Rdo_Query object, or an Rdo object.
exists()  : bool
Check if at least one object matches $query.
find()  : mixed
find() can be called in several ways.
findOne()  : mixed
findOne can be called in several ways.
map()  : Base
Create an instance of $this->_classname from a set of data.
mapFields()  : mixed
Update an instance of $this->_classname from a set of data.
mapperToEntity()  : string
Transform this mapper's class name to an entity class name.
mapperToTable()  : string
Transform this mapper's class name to a database table name.
removeRelation()  : int
Removes a relation to one of the relationships defined in the mapper.
setFactory()  : Mapper
Attach a Factory to the mapper.
sortBy()  : mixed
Set a default sort rule for all queries done with this Mapper.
tableToMapper()  : Mapper
Transform a table name to a mapper class name.
update()  : int
Updates a record in the backend. $object can be either a primary key or an Rdo object. If $object is an Rdo instance then $fields will be ignored as values will be pulled from the object.

Properties

$_classname

What class should this Mapper create for objects? Defaults to the Mapper subclass' name minus "Mapper". So if the Rdo_Mapper subclass is UserMapper, it will default to trying to create User objects.

protected string $_classname

$_defaultSort

Default sorting rule to use for all queries made with this mapper. This is a SQL ORDER BY fragment (without 'ORDER BY').

protected string $_defaultSort

$_lazyFields

Fields that should only be read from the database when they are accessed.

protected array<string|int, mixed> $_lazyFields = array()

$_lazyRelationships

Relationships that should only be read from the database when they are accessed.

protected array<string|int, mixed> $_lazyRelationships = array()

$_relationships

Relationships for this entity.

protected array<string|int, mixed> $_relationships = array()

$_setTimestamps

If this is true and fields named created_at and updated_at are present, Rdo will automatically set creation and last updated timestamps.

protected bool $_setTimestamps = true

Timestamps are always GMT for portability.

$_tableDefinition

The definition of the database table (or view, etc.) that holds this Mapper's objects.

protected Horde_Db_Adapter_Base_TableDefinition $_tableDefinition

Methods

__construct()

public __construct(Horde_Db_Adapter $adapter) : mixed
Parameters
$adapter : Horde_Db_Adapter
Return values
mixed

__get()

Provide read-only, on-demand access to several properties. This method will only be called for properties that aren't already present; once a property is fetched once it is cached and returned directly on any subsequent access.

public __get(string $key) : mixed

These properties are available:

adapter: The Horde_Db_Adapter this mapper is using to talk to the database.

factory: The Factory instance, if present

inflector: The Horde_Support_Inflector this Mapper uses to singularize and pluralize PHP class, database table, and database field/key names.

table: The database table or view that this Mapper manages.

tableDefinition: The Horde_Db_Adapter_Base_TableDefinition object describing the table or view this Mapper manages.

fields: Array of all field names that are loaded up front (eager loading) from the table.

lazyFields: Array of fields that are only loaded when accessed.

relationships: Array of relationships to other Mappers.

lazyRelationships: Array of relationships to other Mappers which are only loaded when accessed.

Parameters
$key : string

Property name to fetch

Return values
mixed

Value of $key

addRelation()

Adds a relation.

public addRelation(string $relationship, Base $ours, Base $theirs) : mixed
  • For one-to-one relations, simply updates the relation field.
  • For one-to-many relations, updates the related object's relation field.
  • For many-to-many relations, adds an entry in the "through" table.
  • Performs a no-op if the peer is already related.
Parameters
$relationship : string

The relationship key in the mapper.

$ours : Base

The object from this mapper to add the relation.

$theirs : Base

The other object from any mapper to add the relation.

Tags
throws
RdoException
Return values
mixed

count()

Count objects that match $query.

public count([mixed $query = null ]) : int
Parameters
$query : mixed = null

The query to count matches of.

Return values
int

All objects matching $query.

create()

Create a new object in the backend with $fields as initial values.

public create(array<string|int, mixed> $fields) : Base
Parameters
$fields : array<string|int, mixed>

Array of field names => initial values.

Return values
Base

The newly created object.

delete()

Deletes a record from the backend. $object can be either a primary key, an Rdo_Query object, or an Rdo object.

public delete(string|Base|Query $object) : int
Parameters
$object : string|Base|Query

The Rdo object, Query, or unique id to delete.

Return values
int

Number of objects deleted.

exists()

Check if at least one object matches $query.

public exists(mixed $query) : bool
Parameters
$query : mixed

Either a primary key, an array of keys => values, or a Query object.

Return values
bool

True or false.

find()

find() can be called in several ways.

public find([mixed $arg = null ]) : mixed

Primary key mode: pass find() a numerically indexed array of primary keys, and it will return a list of the objects that correspond to those keys.

If you pass find() no arguments, all objects of this type will be returned.

If you pass find() an associative array, it will be turned into a Query object.

If you pass find() a Query, it will return a list of all objects matching that query.

Parameters
$arg : mixed = null
Return values
mixed

findOne()

findOne can be called in several ways.

public findOne([mixed $arg = null ]) : mixed

Primary key mode: pass find() a single primary key, and it will return a single object matching that primary key.

If you pass findOne() no arguments, the first object of this type will be returned.

If you pass findOne() an associative array, it will be turned into a Query object.

If you pass findOne() a Query, it will return the first object matching that query.

Parameters
$arg : mixed = null
Return values
mixed

map()

Create an instance of $this->_classname from a set of data.

public map([array<string|int, mixed> $fields = array() ]) : Base
Parameters
$fields : array<string|int, mixed> = array()

Field names/default values for the new object.

Tags
see

$_classname

Return values
Base

An instance of $this->_classname with $fields as initial data.

mapFields()

Update an instance of $this->_classname from a set of data.

public mapFields(Base $object[, array<string|int, mixed> $fields = array() ]) : mixed
Parameters
$object : Base

The object to update

$fields : array<string|int, mixed> = array()

Field names/default values for the object

Return values
mixed

mapperToEntity()

Transform this mapper's class name to an entity class name.

public mapperToEntity() : string
Return values
string

A Base concrete class name if the class exists, else null.

mapperToTable()

Transform this mapper's class name to a database table name.

public mapperToTable() : string
Return values
string

The database table name.

removeRelation()

Removes a relation to one of the relationships defined in the mapper.

public removeRelation(string $relationship, Base $ours[, Base $theirs = null ]) : int
  • For one-to-one and one-to-many relations, simply sets the relation field to 0.
  • For many-to-many, either deletes all relations to this object or just the relation to a given peer object.
  • Performs a no-op if the peer is already unrelated.

This is a proxy to the mapper's removeRelation method.

Parameters
$relationship : string

The relationship key in the mapper.

$ours : Base

The object from this mapper.

$theirs : Base = null

The object to remove from the relation.

Tags
throws
RdoException
Return values
int

the number of affected relations

setFactory()

Attach a Factory to the mapper.

public setFactory([Factory $factory = null ]) : Mapper

If called without arguments, detaches the mapper from factory

Parameters
$factory : Factory = null

A Factory instance or null

Return values
Mapper

this mapper

sortBy()

Set a default sort rule for all queries done with this Mapper.

public sortBy(string $sort) : mixed
Parameters
$sort : string

SQL sort fragment, such as 'updated DESC'

Return values
mixed

tableToMapper()

Transform a table name to a mapper class name.

public tableToMapper(string $table) : Mapper
Parameters
$table : string

The database table name to look up.

Return values
Mapper

A new Mapper instance if it exists, else null.

update()

Updates a record in the backend. $object can be either a primary key or an Rdo object. If $object is an Rdo instance then $fields will be ignored as values will be pulled from the object.

public update(string|Rdo $object[, array<string|int, mixed> $fields = null ]) : int
Parameters
$object : string|Rdo

The Rdo instance or unique id to update.

$fields : array<string|int, mixed> = null

If passing a unique id, the array of field properties to set for $object.

Return values
int

Number of objects updated.

Search results