Download the PHP package stellarwp/models without Composer
On this page you can find all versions of the php package stellarwp/models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stellarwp/models
More information about stellarwp/models
Files in stellarwp/models
Package models
Short Description A library for a simple model structure.
License GPL-3.0-or-later
Homepage https://github.com/stellarwp/models
Informations about the package models
StellarWP Models
A library for a simple model structure.
Table of Contents
- Installation
- Notes on examples
- Configuration
- Creating a model
- Interacting with a model
- Data transfer objects
- Classes of note
- Model
- ModelFactory
- ModelQueryBuilder
- DataTransferObject
- Repositories\Repository
- Contracts of note
- Contracts\ModelCrud
- Contracts\ModelHasFactory
- Contracts\ModelReadOnly
- Repositories\Contracts\Deletable
- Repositories\Contracts\Insertable
- Repositories\Contracts\Updatable
Installation
It's recommended that you install Schema as a project dependency via Composer:
We actually recommend that this library gets included in your project using Strauss.
Luckily, adding Strauss to your
composer.json
is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.
Notes on examples
Since the recommendation is to use Strauss to prefix this library's namespaces, all examples will be using the Boomshakalaka
namespace prefix.
Configuration
This library requires some configuration before its classes can be used. The configuration is done via the Config
class.
Creating a model
Models are classes that hold data and provide some helper methods for interacting with that data.
A simple model
This is an example of a model that just holds properties.
A ReadOnly model
This is a model whose intent is to only read and store data. The Read operations should - in most cases - be deferred to
a repository class, but the model should provide a simple interface for interacting with the repository. You can create
ReadOnly model by implementing the Contracts\ModelReadOnly
contract.
A CRUD model
This is a model that includes CRUD operations. Ideally, the actual CRUD operations should be deferred to and handled by
a repository class, but the model should provide a simple interface for interacting with the repository. We get a CRUD
model by implementing the Contracts\ModelCrud
contract.
Attribute validation
Sometimes it would be helpful to validate attributes that are set in the model. To do that, you can create validate_*()
methods that will execute any time an attribute is set.
Here's an example:
Data Transfer Objects
Data Transfer Objects (DTOs) are classes that help with the translation of database query results (or other sources of data)
into models. DTOs are not required for using this library, but they are recommended. Using these objects helps you be more
deliberate with your query usage and allows your models and repositories well with the ModelQueryBuilder
.
Here's an example of a DTO for breakfasts:
Repositories
Repositories are classes that fetch from and interact with the database. Ideally, repositories would be used to
query the database in different ways and return corresponding models. With this library, we provide
Deletable
, Insertable
, and Updatable
contracts that can be used to indicate what operations a repository provides.
You may be wondering why there isn't a Findable
or Readable
contract (or similar). That's because the fetching needs
of a repository varies with the usecase. However, in the Repository
abstract class, there is an abstract prepareQuery()
method. This method should return a ModelQueryBuilder
instance that can be used to fetch data from the database.
Interacting with the Repository
Querying
Inserting
Updating
Deleting
Classes of note
Model
This is an abstract class to extend for your models.
ModelFactory
This is an abstract class to extend for creating model factories.
ModelQueryBuilder
This class extends the stellarwp/db
QueryBuilder
class so that it returns
model instances rather than arrays or stdClass
instances. Using this requires models that implement the ModelFromQueryBuilderObject
interface.
DataTransferObject
This is an abstract class to extend for your DTOs.
Repositories\Repository
This is an abstract class to extend for your repositories.
Contracts of note
Contracts\ModelCrud
Provides definitions of methods for CRUD operations in a model.
Contracts\ModelHasFactory
Provides definition for factory methods within a model.
Contracts\ModelReadOnly
Provides method signatures for read operations in a model.
Repositories\Contracts\Deletable
Provides method signatures for delete methods in a repository.
Repositories\Contracts\Insertable
Provides method signatures for insert methods in a repository.
Repositories\Contracts\Updatable
Provides method signatures for update methods in a repository.