Download the PHP package aternos/model without Composer
On this page you can find all versions of the php package aternos/model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aternos/model
More information about aternos/model
Files in aternos/model
Informations about the package model
PHP Model
PHP library for simple and complex database models.
About
This library was created to provide a flexible, but yet easy to use Model system, which can be used for new projects but also integrated into existing projects. Every part of the library can be overwritten and replaced separately for custom logic.
In contrast to other model libraries, this library is not tied to any specific database backend, instead every model can contain custom logic for accessing different databases, e.g. for a different cache or search backend. Therefore, this library does not provide any provisioning functionality.
Note: This library is still in development and some features especially regarding more driver functions will be added in the future.
Installation
Basic usage
Driver
The library includes some drivers, but you can also create your own drivers and use them in your project or submit them to be added to the library.
Currently included drivers are:
- Redis
- Cassandra
- Mysqli
- Elasticsearch
All of these drivers require additional extensions or packages, see "suggest" in composer.json.
Most drivers will work out of the box with a local database set up without password, but for most use cases you have to use different credentials. To do that with the included drivers, you can create a new instance and set the credentials using the constructor
or fluent setters
or create a new driver class extending the library driver and overwrite the protected credential properties (either in the class itself or in the constructor), e.g.:
After that you have to register the class in the DriverRegistry:
All drivers have an ID to identify and if necessary overwrite existing drivers. Usually it is recommended to use a
unique ID for each driver type, but if you need multiple drivers of the same type (e.g. if you have two different Mysql
databases), you can create and register multiple drivers with different IDs. You can set the ID using the setId()
fluent
setter on any driver or by overwriting the getId()
function in your own class.
Model
Now you can create a model class. All model classes have to follow the ModelInterface. This library includes two different abstract model classes to make the model creation easier:
- BaseModel - Implements the basic model logic and is not related to any Driver
- GenericModel - Optional implementation of all drivers and registry
It's recommended to start with the GenericModel since it already implements multiple drivers and you can enable which drivers you need for each model or for all models (by using your own parent model for all your models).
This is an example implementation of a model using the GenericModel with a Mysqli database
as backend and caching. The driver configuration is an ordered array $drivers
with driver IDs, the first driver is used first
to get the model. The drivers for the other actions depend on the $drivers
array (e.g. reversed for save()
and
delete()
), but can be individually configured, e.g. setting the $saveDrivers
array.
Use your model
You can now use your model in your code:
Query
You can query the model using a SelectQuery. It allows different syntax possibilities such as simple array/string/int values directly passed to the constructor or building all parameters as objects based on the QueryResult object, which is iterable and countable.
Select
Update
Delete
Testing
This library includes a TestDriver
which can be used to write tests without a database.
It uses a simple array as storage and is not persistent. It supports most basic operations and queries, but might not work
for all use cases yet especially not for database specific queries.
You can just add test data to your model which will also enable the test driver for that model.
Alternatively, you can also add data to the test driver directly.
If you add data to the driver directly, you still have to enable the test driver for each model that you want to test.
Advanced usage
More information about more advanced usage, such as writing your own drivers, driver factory or models will be added in the future, in the meantime just take a look at the source code.
All versions of model with dependencies
ext-json Version *