Now I got the answer, so I answer myself.
After initializing redbean, you can configure a new toolbox. The toolbar in redbean handles 3 important objects: a query writer, Redbean OODB, and a database adapter. You can access the current redbean toolbar with R::$toolbox
You can do this code:
R::configureFacadeWithToolbox(new RedBean_ToolBox(R::$redbean, R::$adapter, R::$writer));
This code does nothing. Since you are customizing Redbean using the new toolbar, but with the same OODB, the same database adapter, and the same query author. But in this code, you can replace one of these objects with your own object.
Example, replacing the writer with a mannequin:
$writer = new MyQueryWriter(); R::configureFacadeWithToolbox(new RedBean_ToolBox(R::$redbean, R::$adapter, $writer));
The bottom line is this:
- You want to replace the query creator with your own query author to handle the table prefix
- The request record class is not always the same. Redbean uses 5 classes for a query writer. The class depends on the type of database. For example, if you are using a Mysql database, the query record class is
RedBean_QueryWriter_MySQL - You do not want to write an entire query writer.
The following receptor writer scenarios are possible:
- RedBean_QueryWriter_CUBRID
- RedBean_QueryWriter_MySQL
- RedBean_QueryWriter_Oracle
- RedBean_QueryWriter_PostgreSQL
- RedBean_QueryWriter_SQLiteT
So this is my decision. I wrote 5 classes with few characters.
class MyCubridQueryWriter extends RedBean_QueryWriter_CUBRID { public function safeTable($name, $noQuotes = false) { $name = prefix($name); return parent::safeTable($name, $noQuotes); } } class MyMysqlQueryWriter extends RedBean_QueryWriter_MySQL { public function safeTable($name, $noQuotes = false) { $name = prefix($name) return parent::safeTable($name, $noQuotes); } } class MyOracleQueryWriter extends RedBean_QueryWriter_Oracle { public function safeTable($name, $noQuotes = false) { $name = prefix($name) return parent::safeTable($name, $noQuotes); } } class MyPostgreSqlQueryWriter extends RedBean_QueryWriter_PostgreSQL { public function safeTable($name, $noQuotes = false) { $name = prefix($name) return parent::safeTable($name, $noQuotes); } } class MySQLiteTQueryWriter extends RedBean_QueryWriter_SQLiteT { public function safeTable($name, $noQuotes = false) { $name = prefix($name) return parent::safeTable($name, $noQuotes); } }
As you can see, each class extends the Redbean query record class. We override the safeTable method. Redbean always uses safeTable for the table name. The prefix function is simple:
function prefix($table) { return "my_prefix_$table"; }
So now in our code. We can use the array to map the Redbean query writer class to our own classes and replace it. Here we are:
$writerMapping = array( 'RedBean_QueryWriter_CUBRID' => 'MyCubridQueryWriter', 'RedBean_QueryWriter_MySQL' => 'MyMysqlQueryWriter', 'RedBean_QueryWriter_Oracle' => 'MyOracleQueryWriter', 'RedBean_QueryWriter_PostgreSQL' => 'MyPostgreSqlQueryWriter', 'RedBean_QueryWriter_SQLiteT' => 'MySQLiteTQueryWriter' ); $class = $writerMapping[get_class(R::$writer)]; $writer = new $class(R::$adapter); R::configureFacadeWithToolbox(new RedBean_ToolBox(R::$redbean, R::$adapter, $writer));
Et voila. Now Redbean will use your own writer, and you can do what you want! Using our safeTable method safeTable we add a prefix to each table name in the database.