-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15 Model update static functionality.
- Loading branch information
Showing
2 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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() | ||
); | ||
} | ||
} |