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:
tableName() method to change this.id. Override the primaryKey() method to change this.\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.
tableName() : string
Defaults to the plural form of the class name (without a namespace prefix).
string
primaryKey() : string
Defaults to 'id'.
string
defaultPDO() : \HotMelt\PDO
save() : boolean
For newly inserted objects, this will also set the primary key property.
booleantrue if the changes can be saved, false if not.
insert(array $properties = false, \HotMelt\PDO $pdo = null) : mixed
arrayThe 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.
mixedThe newly inserted object if changes could be saved, false if not.
delete()
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() |
|---|---|
stringThe name of the property whose value you wish to retrieve.
callableA callback function that computes the property value and returns it. This function should not take any parameters.
mixed
purgeCachedProperty(string $key)
| see | \HotMelt\PersistentObject::getCachedProperty() |
|---|---|
stringThe name of the property whose value you wish to purge from the cache.