Skip to content

Commit 9b814bb

Browse files
committed
#15 Model update static functionality.
1 parent 1984020 commit 9b814bb

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Traits/DataModelTrait.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author 10 Quality <[email protected]>
1111
* @license MIT
1212
* @package wp-query-builder
13-
* @version 1.0.9
13+
* @version 1.0.12
1414
*/
1515
trait DataModelTrait
1616
{
@@ -155,4 +155,21 @@ function( $attributes ) {
155155
->get( ARRAY_A )
156156
);
157157
}
158+
/**
159+
* Returns query results from mass update.
160+
* @since 1.0.12
161+
*
162+
* @param array $set Set of column => data to update.
163+
* @param array $where Where condition.
164+
*
165+
* @return \TenQuality\WP\Database\Abstracts\DataModel|null
166+
*/
167+
public static function update_all( $set, $where = [] )
168+
{
169+
$builder = new QueryBuilder( self::TABLE . '_static_update' );
170+
return $builder->from( self::TABLE )
171+
->set( $set )
172+
->where( $where )
173+
->update();
174+
}
158175
}

tests/cases/TraitModelTest.php

+41-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author 10 Quality <[email protected]>
99
* @license MIT
1010
* @package wp-query-builder
11-
* @version 1.0.7
11+
* @version 1.0.12
1212
*/
1313
class TraitModelTest extends TestCase
1414
{
@@ -112,4 +112,44 @@ public function testAll()
112112
$this->assertInternalType( 'array', $collection );
113113
$this->assertInstanceOf( 'Model', $collection[0] );
114114
}
115+
/**
116+
* Test abstract
117+
* @since 1.0.12
118+
* @group model
119+
* @group trait
120+
* @group update
121+
*/
122+
public function testUpdate()
123+
{
124+
// Preapre
125+
global $wpdb;
126+
// Exec
127+
$flag = Model::update_all( ['status' => 'active'] );
128+
// Assert
129+
$this->assertInternalType( 'bool', $flag );
130+
$this->assertTrue( $flag );
131+
$this->assertEquals(
132+
'UPDATE prefix_' . Model::TABLE . ' SET status = %s',
133+
$wpdb->get_query()
134+
);
135+
}
136+
/**
137+
* Test abstract
138+
* @since 1.0.12
139+
* @group model
140+
* @group trait
141+
* @group update
142+
*/
143+
public function testUpdateWhere()
144+
{
145+
// Preapre
146+
global $wpdb;
147+
// Exec
148+
$flag = Model::update_all( ['status' => 'active'], ['type' => 'yolo'] );
149+
// Assert
150+
$this->assertEquals(
151+
'UPDATE prefix_' . Model::TABLE . ' SET status = %s WHERE type = %s',
152+
$wpdb->get_query()
153+
);
154+
}
115155
}

0 commit comments

Comments
 (0)