Skip to content

Commit bc6ab9c

Browse files
committed
Introduce truncate method
1 parent 4dc4939 commit bc6ab9c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Schema/Tables/Contracts/Table.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,28 @@ public function empty_table() {
220220

221221
$this_table = static::table_name( true );
222222

223+
$this->db::query( "SET foreign_key_checks = 0" );
224+
$result = $this->db::query( "DELETE FROM {$this_table}" );
225+
$this->db::query( "SET foreign_key_checks = 1" );
226+
227+
return $result;
228+
}
229+
230+
/**
231+
* Truncates the custom table.
232+
*
233+
* @since TBD
234+
*
235+
* @return int|false The number of removed rows, or `false` to indicate a failure.
236+
*/
237+
public function truncate() {
238+
if ( ! $this->exists() ) {
239+
// There is really nothing to empty here.
240+
return 0;
241+
}
242+
243+
$this_table = static::table_name( true );
244+
223245
$this->db::query( "SET foreign_key_checks = 0" );
224246
$result = $this->db::query( "TRUNCATE {$this_table}" );
225247
$this->db::query( "SET foreign_key_checks = 1" );

0 commit comments

Comments
 (0)