RedBeanPHP

easy ORM for PHP

Custom getters

Besides, properties, nested beans and nested lists a bean may also contain custom getters. If you want to map a relation which is not supported by RedBeanPHP you can use this technique.

For instance, consider the retrieval of historical data. We have a $person bean and our task is to get all the related $events sorted by date. What RedBeanPHP can do is retrieve all events like this

 
$person
->ownEvents 

But that might not be the correct order! In this case we better write our own getter:


    
class Model_Person extends RedBean_SimpleModel {
        public function 
getEvents() {
            return 
R::find('event',' person_id = ? order by `date` asc',array($this->id));
        }
    }

To use this custom getter:


    $person 
R::load('person',$certainID);
    
$eventBeans $person->getEvents();

Notice that RedBeanPHP knows that Model_Person belongs to bean $person. This is called FUSE because the bean and the model are fused. Read more about models and fuse.

The flexibility of FUSE allows RedBeanPHP to be used as a stand-alone ORM solution, the BeanCan Server



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.010779857635498 sec.