Redbeanphp and table prefix

I am using Redbeanphp ( http://redbeanphp.com/ ) in my php project. And I want to use a table prefix for my tables.

Redbeanphp does not support table prefix since version 3.0. But I want to extend Redbeanphp to support the table prefix in my project.

I do not want to change the redbeanphp code. But if there is no solution, I will do it.

I already tried replacing QueryWriter Redbeanphp, but the QueryWriter class is not always the same (it depends on the type of my database).

What is the best way to do this?

+4
source share
3 answers

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.

+6
source

I came across this problem when I want to use RedBean with Wordpress. My solution was to create another class (WPR for "wordpress redbean"), for example:

 class WPR { public static function __callStatic($method, $params) { global $wpdb; $prefix = $wpdb->base_prefix; foreach ($params as &$param) $param = preg_replace('/\{([a-zA-Z0-9_]+)\}/', $prefix . '$1', $param); // switch to wordpress database R::selectDatabase('WPR'); // do the dang thing $res = call_user_func_array(array('R',$method),$params); // go back R::selectDatabase('default'); // send it return $res; } }; R::addDatabase('WPR', "mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD); 

I also wanted this class to use a different database than my "regular" redbean class, so I have selectDatabase () calls. Comment on them if you do not need them.

What he does is he acts as a proxy server for redbean, but with each input he checks some substring like {this} and extends it to the full database name with a prefix. Here is an example of your use: $my_blog = WPR::find('{blogs}', 'domain=?', array('mydomain.com')); or $allowed_hosts = WPR::getCol('SELECT domain FROM {blogs}');

In these two cases {blogs} is converted to wp_blogs

0
source

Magus

I have the same problem as you. I tried your solution, but could not get it to work. I wrote several functions for the prefix and the names of my objects in the table names and vice versa, which, I think, will work in my case, but I still want to work, as it will be more transparent. I have unsigned table names that work for reading and writing.

I noticed that Oracle support is not available out of the box in RedBean, so I added checks for each class name to avoid errors:

 if (class_exists('RedBean_QueryWriter_MySQL', false)) { class MyMysqlQueryWriter extends RedBean_QueryWriter_MySQL { ... } 

Checks should work, I got output to my log in my MySQL block (which I use) when loading the prefix code.

In addition, at the end you wrote:

 $class = $writerMapping(get_class(R::$writer)); 

but you probably meant:

 $class = $writerMapping[get_class(R::$writer)]; 

Based on some debugging, my R::$writer was changed after configureFacadeWithToolbox , but for some reason the table names are not converted and nothing of my user- safeTable function is executed.

If you could give more detailed information on how you tested your method or what I might lose, I would be glad to hear it.

(I'm sorry this post is not the answer to your question, but I really couldn't find another way to send you a message or comment on your answer. Damn stack overflow! (Just kidding, I love that.))

0
source

All Articles