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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package models

StellarWP Models

A library for a simple model structure.

Table of Contents

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. Properties can be defined in one or both of the following ways:

Using the shorthand syntax:

Using property definitions for more control:

Property definition options:

A persistable model

This is a model that includes persistence operations (create, find, save, delete). Ideally, the actual persistence 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 persistable model by implementing the Contracts\ModelPersistable contract.

Interacting with a model

Change tracking

Models track changes to their properties and provide methods to manage those changes:

Checking if properties are set

The isSet() method checks if a property has been set. This is different from PHP's isset() because it considers null values and default values as "set":

Key differences from PHP's isset():

Creating models from query data

Models can be created from database query results using the fromData() method:

Build modes:

Readonly properties

Properties marked as readonly() can only be set during construction and cannot be modified afterward:

Readonly properties are useful for:

Extending model construction

Models can perform custom initialization after construction by overriding the afterConstruct() method:

Model Relationships

Models can define relationships to other models, similar to how properties are defined. Relationships support lazy loading and caching.

Defining Relationships

Relationships can be defined using either shorthand syntax or the fluent ModelRelationshipDefinition API:

Using shorthand syntax:

Using relationship definitions for more control:

Relationship Types

Five relationship types are available:

Accessing Relationships

Relationships are loaded lazily when accessed as properties:

Relationship Caching

By default, relationships are cached after the first load. You can control caching behavior:

Managing Relationship Cache

Models provide methods to manage relationship caching:

Customizing Relationship Loading

Override the fetchRelationship() method to customize how relationships are loaded:

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

Unsetting properties

Classes of note

Model

This is an abstract class to extend for your models.

ModelQueryBuilder

This class extends the stellarwp/db QueryBuilder class so that it returns model instances rather than arrays or stdClass instances.

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\ModelPersistable

Provides definitions of methods for persistence operations in a model (create, find, save, delete, query).

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.


All versions of models with dependencies

PHP Build Version
Package Version
Requires stellarwp/db Version ^1.0.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package stellarwp/models contains the following files

Loading the files please wait ...