4

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 cumbersome.

4 Answers 4

6

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

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

1
  • after getting the adapter, you'd do: $adapter->exec('TRUNCATE TABLE yourtable');
    – Erik
    Commented Dec 3, 2010 at 17:58
1

Wipe a single table like this:

R::wipe($table);

Wipe all tables in an MySQL schema like this:

function CleanAllTables() {
    $tables = R::getCol(' show tables ');
    foreach ($tables as $table) {
        R::wipe($table);
    }
}

MySQL:

TRUNCATE TABLE <table_name>

Executed with the RedBean Adapter

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

this should do the job! :)

1

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');

Not the answer you're looking for? Browse other questions tagged or ask your own question.