Anchor ORM
Anchor will be a new minimal ORM to specifically handle the retrieval of records (arrays) from PDO data sources and then the subsequent hydration of them into objects, which may be business domains.
Anchor will not handle inserts or deletions; it purely handles fetches, as business domains tend to require complex insertion logic which we cannot always reliably handle.
Anchor will place no constraints whatsoever on the business domain objects. Instead consumers will create AnchorResource
definitions which are used by Anchor to fetch data. Example AnchorResource
interface:
-
key() : string
- get database primary key field (used when joined as a relation) -
table() : string
- get database table name -
fields() : array
- get array of field names which Anchor should select from the resource's database table (table()
) -
relations() : AnchorRelationCollection
- define relations to otherAnchorResource
instances (OTO/OTM supported, recursive relations will not be supported at this time) -
collection() : AnchorCollection
- create a new collection to contain hydrated instances of the resource's object (fetched from the database) hydrate(array $record) : object
It can be observed that each resource must implement its own hydration routine; Anchor is not aware of any aspect of the business domain's operation and so resources must hydrate their own domains. Anchor will pass as $record
an array containing the value fetched from the database for each field listed in fields()
. Anchor will also abstract relation hydration - by the time hydrate()
is called for a resource, $record
will be populated with the output of hydrate()
from each relation resource in relation()
which was included in the record. Therefore each resource merely needs to implement hydrate()
to map to the business domain and does not need to handle recursive mapping.
The Anchor interface will have two methods:
-
get(AnchorResource $resource, SQL $sql) : object
- fetch a single domain -
fetch(AnchorResource $resource, SQL $sql=null) : AnchorCollection
- fetch multiple
It is crucial to acknowledge that Anchor is expressly intended to be used with PDO only and therefore has a dependence on SQL queries, as evidenced from the interface above. We only implement fetching methods and Anchor should be used as a part of a wider repository pattern in the consuming app which should then implement e.g. insert()
using its knowledge of the business domain.