Skip to content

Commit

Permalink
#15 Model update static functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
amostajo committed Aug 25, 2020
1 parent 1984020 commit 9b814bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/Traits/DataModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author 10 Quality <[email protected]>
* @license MIT
* @package wp-query-builder
* @version 1.0.9
* @version 1.0.12
*/
trait DataModelTrait
{
Expand Down Expand Up @@ -155,4 +155,21 @@ function( $attributes ) {
->get( ARRAY_A )
);
}
/**
* Returns query results from mass update.
* @since 1.0.12
*
* @param array $set Set of column => data to update.
* @param array $where Where condition.
*
* @return \TenQuality\WP\Database\Abstracts\DataModel|null
*/
public static function update_all( $set, $where = [] )
{
$builder = new QueryBuilder( self::TABLE . '_static_update' );
return $builder->from( self::TABLE )
->set( $set )
->where( $where )
->update();
}
}
42 changes: 41 additions & 1 deletion tests/cases/TraitModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author 10 Quality <[email protected]>
* @license MIT
* @package wp-query-builder
* @version 1.0.7
* @version 1.0.12
*/
class TraitModelTest extends TestCase
{
Expand Down Expand Up @@ -112,4 +112,44 @@ public function testAll()
$this->assertInternalType( 'array', $collection );
$this->assertInstanceOf( 'Model', $collection[0] );
}
/**
* Test abstract
* @since 1.0.12
* @group model
* @group trait
* @group update
*/
public function testUpdate()
{
// Preapre
global $wpdb;
// Exec
$flag = Model::update_all( ['status' => 'active'] );
// Assert
$this->assertInternalType( 'bool', $flag );
$this->assertTrue( $flag );
$this->assertEquals(
'UPDATE prefix_' . Model::TABLE . ' SET status = %s',
$wpdb->get_query()
);
}
/**
* Test abstract
* @since 1.0.12
* @group model
* @group trait
* @group update
*/
public function testUpdateWhere()
{
// Preapre
global $wpdb;
// Exec
$flag = Model::update_all( ['status' => 'active'], ['type' => 'yolo'] );
// Assert
$this->assertEquals(
'UPDATE prefix_' . Model::TABLE . ' SET status = %s WHERE type = %s',
$wpdb->get_query()
);
}
}

0 comments on commit 9b814bb

Please sign in to comment.