Skip to content

Handling A Data Model

Ale Mostajo edited this page Aug 25, 2020 · 4 revisions

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.


Insert

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;

Find

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 );

Find Where

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'] );

Delete Where

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] );

Where

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] );

All

static::all()

Returns: array the collection with all records found.

Usage sample:

$models = MyModel::all();

Count

static::count( $args )

Parameter Type Description
$args array Where statement arguments used return count.

Returns: int.

Usage sample:

$count = MyModel::count( ['type_id' => 1] );

Builder

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' );

Save

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';
}

Delete

Deletes an instantiated model.

delete()

Returns: bool.

Usage sample:

$model = MyModel::find( 1 );
if ( $model->delete() ) {
   echo 'deleted';
}

Update

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';
}

Update All

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';
}