Skip to content Skip to sidebar Skip to footer

Empty Or Truncate Table With Redbean Php?

I'm using RedBean PHP for testing purposes and I like it very much, however I have no idea how I can truncate a table. I can fetch all the beans and delete them, but that seems cum

Solution 1:

In RedBean 1.3 you can use R::wipe($type) to truncate a table.

Solution 2:

RedBean is just an ORM tool (AFAIK) so if your back-end database is SQL based, you can simply do an SQL statement like: TRUNCATE TABLE yourTable;

To execute queries directly via RedBean

The Adapter

The adapter is the class that communicates with the database for RedBean. This adapter makes it possible to execute queries to manipulate the database. To get an instance of this adapter use:

$adapter = $toolbox->getDatabaseAdapter();

from http://www.redbeanphp.com/downloads/redbean.pdf - 1.3 http://www.redbeanphp.com/manual/manual.pdf - 2.0

Solution 3:

Wipe a single table like this:

R::wipe($table);

Wipe all tables in an MySQL schema like this:

functionCleanAllTables() {
    $tables = R::getCol(' show tables ');
    foreach ($tablesas$table) {
        R::wipe($table);
    }
}

MySQL:

TRUNCATETABLE<table_name>

Executed with the RedBean Adapter

$adapter->exec('TRUNCATE TABLE <table_name>');

this should do the job! :)

Solution 4:

Old question, but for what it's worth, RedBean 4.0+ (and possibly older versions too) makes use of foreign keys when one makes use of the various N-M relational mappings.

So, in this case, in my case, in order to work around this, it was necessary to set foreign key checks to 0.

R::exec('SET FOREIGN_KEY_CHECKS = 0;');
R::wipe('tablename');

Post a Comment for "Empty Or Truncate Table With Redbean Php?"