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
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
-
after getting the adapter, you'd do:
$adapter->exec('TRUNCATE TABLE yourtable');
– ErikCommented Dec 3, 2010 at 17:58
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! :)
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');