-
Notifications
You must be signed in to change notification settings - Fork 4
Handling A Data Model
List of static and public methods to handle a model extended from the DataModel class.
NOTE: Samples in this article will be consistent with the model shown in the data model's wiki article.
static::insert( $attributes )
| Parameter | Type | Description |
|---|---|---|
| $attributes | array |
Model attribute values to insert. |
Returns: DataModel the inserted model.
Usage sample:
$model = MyModel::insert( ['name' => 'John Doe'] );
echo $model->model_id;static::find( $id )
| Parameter | Type | Description |
|---|---|---|
| $id | mixed |
ID value stored in the primary key column. |
Returns: DataModel a filled model if found, null if not found.
Usage sample:
$model = MyModel::find( 1 );static::find_where( $args )
| Parameter | Type | Description |
|---|---|---|
| $args | array |
Where statement arguments used to find a record. |
Returns: DataModel the first filled model found based on the where arguments, null if not found.
Usage sample:
$model = MyModel::find_where( ['name' => 'John Doe'] );static::delete_where( $args )
| Parameter | Type | Description |
|---|---|---|
| $args | array |
Where statement arguments used to delete multiple records. |
Returns: bool.
Usage sample:
$success = MyModel::delete_where( ['type_id' => 1] );static::where( $args )
| Parameter | Type | Description |
|---|---|---|
| $args | array |
Where statement arguments used return multiple records. |
Returns: array the collection of records found.
Usage sample:
$models = MyModel::where( ['type_id' => 1] );static::all()
Returns: array the collection with all records found.
Usage sample:
$models = MyModel::all();static::count( $args )
| Parameter | Type | Description |
|---|---|---|
| $args | array |
Where statement arguments used return count. |
Returns: int.
Usage sample:
$count = MyModel::count( ['type_id' => 1] );static::builder()
Returns: TenQuality\WP\Database\QueryBuilder A query builder initialized with the from() statement filled with the model's table name (the alias as the table's name).
Usage sample:
$builder = MyModel::builder()->select( 'model_table.name' );Inserts or updates an instantiated model.
save( $force_insert = false )
| Parameter | Type | Description |
|---|---|---|
| $force_insert | bool |
Flag that indicates if should insert regardless of ID. |
Returns: bool.
Usage sample:
$model = MyModel::find( 1 );
// Update
$model->name = 'Jane Doe';
if ( $model->save() ) {
echo 'saved';
}Deletes an instantiated model.
delete()
Returns: bool.
Usage sample:
$model = MyModel::find( 1 );
if ( $model->delete() ) {
echo 'deleted';
}Updates an instantiated model.
update( $data = [])
| Parameter | Type | Description |
|---|---|---|
| $data | array |
The data to update in the model and database (if none is passed, the method will call to save()). |
Returns: bool.
Usage sample:
$model = MyModel::find( 1 );
// Update
if ( $model->update( ['status' => 'active'] ) ) {
echo 'updated';
}Static method that updates many records at once.
update_all( $set, $where = [])
| Parameter | Type | Description |
|---|---|---|
| $set | array |
The set data to update to all records. |
| $where | array |
Where condition to filter which records to update (optional). |
Returns: bool.
Usage sample (updates all records of type cars to status active):
$success = MyModel::update_all( ['status' => 'active'], ['type' => 'cars'] );
// Update
if ( $success ) {
echo 'updated';
}(c) 2019 - 10 Quality - Query Builder Library for Wordpress