Download the PHP package parable-php/orm without Composer
On this page you can find all versions of the php package parable-php/orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download parable-php/orm
More information about parable-php/orm
Files in parable-php/orm
Package orm
Short Description Parable ORM is a small but complete repository-pattern ORM implementation
License MIT
Homepage https://github.com/parable-php/orm
Informations about the package orm
Parable ORM
Parable ORM is a light-weight repository-pattern based ORM.
Install
Php 8.0+ and composer are required.
Usage
Repositories are used to find, save and delete entities, which represent rows from your MySQL/Sqlite database. Parable ORM attempts to combine queries to be as efficient as possible.
Entities are relatively straight-forward PHP data objects, and are expected to offer setters and getters for all values associated with that entity. Example:
As you can see, the entity itself doesn't really need much. The Repository set up for this entity type, however, will contain some metadata so it knows how to handle them.
If you want to support automatic setting of a created at
or updated at
value, it's as simple as implementing either the SupportsCreatedAt
or SupportsUpdatedAt
interfaces. The repository will automatically pick up on it and attempt to call markCreatedAt()
or markUpdatedAt()
, leaving the specific property/column names up to you. Example:
Here's the Repository to handle the above Entity:
As mentioned, both entities and repositories are intended to be as simple and straightforward as possible. Entities and Repositories use parable-php/di
, meaning they can use constructor-based injected dependencies.
Basic Repository use
Condition-based repository use
For advanced (and possibly complex) where conditions, we use a callable
which is given a properly set up Query
object. This allows for fine-grained control with a very low barrier to do so.
This ends up building the following query:
The methods where
, whereNull
, whereNotNull
and whereCallable
will use AND
to combine the clauses. To do otherwise, all have an or
-version. orWhere
, orWhereNull
, orWhereNotNull
, orWhereCallable
, and by using those specifically, an OR
clause can be created.
In many cases you'll want to either only use or
-clauses, or, to use an OR
clause in an otherwise AND
-oriented where list, use a callable
to make sure they're grouped appropriately.
Saving and deleting entities
To save an entity, simply tell the repository to do so:
Or save multiple:
You can also easily defer a save, so that the entity is prepared for a save but not actually saved until you decide to do so. When this is done, all entities that need to be updated are saved individually as they are found in the deferred save list, but all entities that are new (aka to be INSERT
ed) are combined into a single INSERT
query instead.
If $newEntities
contains 10 entities that are all new, one query will now insert all 10 in one go. If, for example, $newEntities
contains 5 new and 5 pre-existing entities, saveDeferred
will build the single INSERT
query for the 5 new ones, and as it comes across them, save the updates immediately.
Deleting works the same:
And deferred deletes are also possible, and will attempt to delete all deferred entities in a single query:
For both deferred saves and deletes, the currently stored entities to be saved or deleted can be cleared by calling either clearDeferredSaves()
or clearDeferredDeletes()
.
How to connect to a database
You didn't think I'd ever forget this, do you? 2 database types are currently supported, MySQL and Sqlite3.
To connect to a MySQL server:
Or:
And to connect to a Sqlite3 file:
Or:
Contributing
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.
License
All Parable components are open-source software, licensed under the MIT license.
All versions of orm with dependencies
ext-pdo Version *
ext-json Version *
parable-php/di Version ^1.0
parable-php/query Version ^1.0