RedBeanPHP

easy ORM for PHP

Custom Mapping

In RedBeanPHP 3.0 this feature is no longer supported. RedBeanPHP is meant for independent developers; those who can model the database after their code. While RedBeanPHP offers some custom mapping features it's not meant to be used the other way around (i.e. model code around schemas). Here I present the functionality that allows you to customize mappings with RedBeanPHP.


    
    
class MyBeanFormatter implements RedBean_IBeanFormatter {
        public function 
formatBeanTable($type) {
            return 
'cms_'.$type;
        }
        public function 
formatBeanID($type) {
            return 
$type.'_id';
        }
        public function 
getAlias($field) {
            if (
$field=='student') return 'person';
            return 
$field;
        }
    }
    
    
R::$writer->setBeanFormatter( new MyBeanFormatter );
    

RedBeanPHP is zero-config. Customizations are done using OOP best practices. In this case we create a formatter class and make it implement the IBeanFormatter interface. Next we set the format class using the setBeanFormatter() method.

For information on the default naming conventions for tables and column, consult the Default Schema chapter.



User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.

Site comments powered by Disqus

page generated in 0.0071451663970947 sec.