Empty or truncate table with RedBean PHP?

徘徊边缘 提交于 2019-12-10 18:24:20

问题


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.


回答1:


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




回答3:


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! :)




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


来源:https://stackoverflow.com/questions/4347970/empty-or-truncate-table-with-redbean-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!