PersistentObject

Implements a lightweight object-relational-mapper interface.

The PersistentObject class prefers convention over configuration. This is an overview of the conventions that the PersistentObject class establishes and how to override them:

  • Objects are read from and written to a table that is the plural form of the subclass name (without the namespace prefix). Override the tableName() method to change this.
  • Objects have a primary key (assumged to be an auto-incrementing integer) named id. Override the primaryKey() method to change this.
  • Some methods allow you to specify a \HotMelt\PDO object for database access. If not PDO is given, the PDO returned from the defaultPDO() class method is used. The default value returned by this method is null, however, so you will have to override this method to use a default PDO.

Note that you should not directly create a new persistent object instance. Use the findBy...() and insert() methods to retrieve existing objects or create new ones.

Methods

Return the name of the table that stores records for objects of this class.

tableName() : string
static

Defaults to the plural form of the class name (without a namespace prefix).

Response

string

Return the name of the primary key property of objects of this class.

primaryKey() : string
static

Defaults to 'id'.

Response

string

Return the default PDO to use, i.e. for methods that let you specify a PDO to use but are called without specifying a PDO.

defaultPDO() : \HotMelt\PDO
static

Defaults to null.

Response

\HotMelt\PDO

Write changes to an object to the database.

save() : boolean

For newly inserted objects, this will also set the primary key property.

Response

boolean

true if the changes can be saved, false if not.

Create a new object and saves it to the database.

insert(array $properties = false, \HotMelt\PDO $pdo = null) : mixed
static

Arguments

$properties

array

The values to assign for the new object's properties. Set to false (rather than an empty array) if you do not wish to assign default values.

$pdo

\HotMelt\PDO

The PDO to associate with the new object.

Response

mixed

The newly inserted object if changes could be saved, false if not.

Delete an object.

delete() 

Retrieve cached property values.

getCachedProperty(string $key, callable $getter) : mixed

Use this method to cache property values that are expensive to compute. You provide the property name along with a getter. When you first call this method, getCachedProperty() will in turn call your getter to compute the value, store the value returned from the getter in the cache and then return the value to you. Subsequent calls to this method will return the value stored in the cache and will not result in the getter being called.

To invalidate a cached property value use the \HotMelt\PersistentObject::purgeCachedProperty() method.

see \HotMelt\PersistentObject::purgeCachedProperty()

Arguments

$key

string

The name of the property whose value you wish to retrieve.

$getter

callable

A callback function that computes the property value and returns it. This function should not take any parameters.

Response

mixed

Invalidates the value for a property cached through `\HotMelt\PersistentObject::getCachedProperty()`.

purgeCachedProperty(string $key) 
see \HotMelt\PersistentObject::getCachedProperty()

Arguments

$key

string

The name of the property whose value you wish to purge from the cache.