abstract class Repository (View source)

Provides functionality to perform basic database operations on models.

Properties

protected Connection $connection

Methods

__construct(Connection $connection)

Repository constructor.

array
find(Schema $schema, array $conditions, array $order = [], int $limit = null)

Returns a list of models from the database based on given conditions.

Model|null
findOne(Schema $schema, array $conditions, array $order = [])

Returns a single model based on the given conditions.

Model|null
findByPrimaryKey(Schema $schema, mixed $values)

Finds a single model based on the primary key.

void
save(Model $model)

Inserts or updates the given model based on its state.

void
insert(Model $model)

Inserts the given model to the database.

void
refresh(Model $model)

Updates the field values for the model from the database.

void
update(Model $model)

Updates the model in the database.

void
delete(Model $model)

Deletes the model from the database.

void
fillRelationships(array $models, array $relationships)

Fills the given relationships for the given models.

query(string $sql)

Returns a custom query object for the given SQL statement.

Details

__construct(Connection $connection)

Repository constructor.

Parameters

Connection $connection The connection to the database

protected array find(Schema $schema, array $conditions, array $order = [], int $limit = null)

Returns a list of models from the database based on given conditions.

Parameters

Schema $schema The schema of the model records
array $conditions Conditions for the select query
array $order Associative list of fields and sorting directions
int $limit Maximum number of results to return for ordered query

Return Value

array List of models returned by the database

protected Model|null findOne(Schema $schema, array $conditions, array $order = [])

Returns a single model based on the given conditions.

Parameters

Schema $schema The schema of the record to select
array $conditions Conditions for the select query
array $order The order for the select query

Return Value

Model|null The found model or null if none was found

protected Model|null findByPrimaryKey(Schema $schema, mixed $values)

Finds a single model based on the primary key.

Parameters

Schema $schema The schema for the selected record
mixed $values Value for single primary key field or list of values for composite primary key

Return Value

Model|null The found model or null if none was found

protected void save(Model $model)

Inserts or updates the given model based on its state.

Parameters

Model $model The model to save

Return Value

void

protected void insert(Model $model)

Inserts the given model to the database.

Parameters

Model $model The model to insert

Return Value

void

protected void refresh(Model $model)

Updates the field values for the model from the database.

Parameters

Model $model The model that should be updated

Return Value

void

protected void update(Model $model)

Updates the model in the database.

Parameters

Model $model The model to update

Return Value

void

protected void delete(Model $model)

Deletes the model from the database.

Parameters

Model $model The model to delete

Return Value

void

protected void fillRelationships(array $models, array $relationships)

Fills the given relationships for the given models.

Parameters

array $models The models to fill with relationships
array $relationships List of relationships to fill for the models

Return Value

void

protected Query query(string $sql)

Returns a custom query object for the given SQL statement.

Parameters

string $sql The sql to use to initialize the query object

Return Value

Query The query object initialized with the connection and the given SQL statement