Any alternatives to the RedBean/Fuse Model naming convention?

php, redbean

Solution

OK, I found one solution while going through the API documentation. I can set my own "Model Formatter", which just means that I can create a class that is given the responsibility of connecting the name of a table with the name of a class. My RedBean setup code now looks like this:

# Redbean
require('vendor/redbean/rb.php');

R::setup("mysql:host=".MYSQL_HOST.";dbname=".MYSQL_DATABASE.";",
         MYSQL_USERNAME, MYSQL_PASSWORD);

# Set custom model formatter
class CustomRedBeanModelFormatter implements RedBean_IModelFormatter
{
    public function formatModel($model)
    {
        switch($model)
        {
            case foo\bar\OmniDataManager::TABLE_DATA:
                return 'foo\bar\OmniDataModel';
            case foo\bar\OmniDataManager::TABLE_GROUP:
                return 'foo\bar\OmniDataGroupModel';
            default:
                return false;
        }
    }
}
$customRedBeanModelFormatter = new CustomRedBeanModelFormatter();
RedBean_ModelHelper::setModelFormatter($customRedBeanModelFormatter);

It's a bit of an ugly solution. I would much rather be able to do something like this:

RedBean_ModelHelper::setModelForTable($modelName, $tableName);

For that reason I will hold off on marking this as the correct answer for a while.

Problem

RedBeanPHP uses class naming convention to tie a model to a table. I can't abide by that naming convention since I have a project where table names aren't set in stone. I need a way to connect a `RedBean_SimpleModel` to a table name without the naming convention - How do I do that?

Original source