Download the PHP package ronanchilvers/orm without Composer
On this page you can find all versions of the php package ronanchilvers/orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package orm
ronanchilvers/orm
orm
is a small and simple database layer implementing the active record pattern. Its aim is to be simple, fast and useful. It relies on the [clancats hydrahon] query builder for database query access.
Overview
Things it does
- PDO underneath so should work with any PDO compatible database
- Provides a full query builder interface using clancats/hydrahon
- Supports full save, destroy, update, insert functionality through the active record implementation
- Supports model finder objects to help avoid 'fat model' syndrome
- Supports fine grained model hooks for precise control of model lifecycle data
- Supports model validation using respect/validation
- Simple relationship handling - belongsTo, hasMany, hasOne
- Configurable data type conversion for model properties
Things it does NOT do
- Migrations - you can use whatever you like to manage your schema. We recommend phinx
- Multiple database connections - currently
orm
only supports a single PDO connection.
Installation
You'll need at least PHP7.0 to use the library. The recommended way to install is using composer:
Configuring the database
Since orm
uses PDO, it's up to you how you create and instantiate your PDO object. This
will probably be in your bootstrap somewhere. Once your PDO object is available you
will need to give it to orm
. Here's an example:
Clearly you will almost certainly not be using a :memory:
DSN in practice. However
you create your PDO object, the crucial point here is that you call Orm::setConnection
to provide orm
with your connection object.
Basic usage
orm
doesn't make any pre-judgments about your database schema. When building models
from a database it assumes it maps columns to properties and when saving it assumes
that any property has a corresponding database table column. Its up to you to make
sure the data makes sense.
Here we assume that we have a database table that looks like the following. We're using MySQL / MariaDB syntax here but whatever PDO supports should be fine.
Defining a model
First create your model class. It should extend the Ronanchilvers\Orm\Model
class. As with many active record implementations we assume here that the table
has a plural name (books) and its corresponding model class will have a singular
name (book).
If your columns have no column prefix, that's all you need to do. The table name will be inferred from the class name. If however, like the example above, you have column prefixes then you can tweak your model to suit.
Similarly if your table doesn't map to the model name you can specufy that too.
Now you're ready to use the model.
Finding models
orm
supports a query builder interface provided by clancats/hydrahon. In order
to retrieve models from the database, first obtain a finder object.
Then you can use the finder object to retrieve models.
There are several standard finder methods you can use:
You can use the full query builder to gain more control over the query:
You can read more about the capabilities of the query builder over at the clancats/hydrahon site.
If you want complete control over the SQL that is executed you can do:
Custom Finder Classes
By default Orm::finder
will give you back a vanilla Ronanchilvers\Orm\Finder
object tied to given model class. However you can also override the finder class for a given model by setting the $finder
static property on your model. This can be very useful if you want to add custom find methods (for example).
Let's imagine you've created a BookFinder
class:
You can see that we've created a subclass of the default finder and we've added a forAuthorId
method which returns an array of books for a given author id.
Then we tell the model about our new finder class:
and then finally you're readdy to use it:
Persistence
Saving a model back to the database is simply a matter of calling the save
method.
Similarly destroying a model is also simple:
Accessing model data
As in any active record implementation, database table columns are expressed as properties on the model instance. Consequently data can be accessed by accessing those properties.
All versions of orm with dependencies
evenement/evenement Version ^3.0
nesbot/carbon Version ^2.0
php Version ^8.0
ronanchilvers/utility Version ^2.0