File tree 2 files changed +59
-2
lines changed
2 files changed +59
-2
lines changed Original file line number Diff line number Diff line change 10
10
* @author 10 Quality <[email protected] >
11
11
* @license MIT
12
12
* @package wp-query-builder
13
- * @version 1.0.9
13
+ * @version 1.0.12
14
14
*/
15
15
trait DataModelTrait
16
16
{
@@ -155,4 +155,21 @@ function( $attributes ) {
155
155
->get ( ARRAY_A )
156
156
);
157
157
}
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
+ }
158
175
}
Original file line number Diff line number Diff line change 8
8
* @author 10 Quality <[email protected] >
9
9
* @license MIT
10
10
* @package wp-query-builder
11
- * @version 1.0.7
11
+ * @version 1.0.12
12
12
*/
13
13
class TraitModelTest extends TestCase
14
14
{
@@ -112,4 +112,44 @@ public function testAll()
112
112
$ this ->assertInternalType ( 'array ' , $ collection );
113
113
$ this ->assertInstanceOf ( 'Model ' , $ collection [0 ] );
114
114
}
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
+ }
115
155
}
You can’t perform that action at this time.
0 commit comments