1
votes

I'm building a custom MVC framework partially based on CakePHP and using RedBeanPHP.

Now I'm in the part of creating the DB CRUD. Here's my idea:

class Model extends RedBean_SimpleModel {

}


class Product extends Model {

var $beans = array(
'name',
'price',
...
);
}

This seems a good way to make RedBeanPHP generate the table "Products" and columns on the fly.

Now my current doubt (since I'm starting to work and understand RedBeanPHP) problem is the associations.

I really like the CakePHP's method, with "hasOne", "hasMany", "belongsTo", etc.

Can someone guide me to a way of doing that but with RedBeanPHP?

2

2 Answers

1
votes

There is no need to use up-front models with RedBeanPHP. RedBeanPHP can connect your beans automatically for you using a system called FUSE. This saves a lot of code. And... less code == less bugs

$p = R::dispense('product');
$p->price = -6; //not a valid price
R::store($p);

Later you want to add validation and you write:

class Model_Product extends RedBean_SimpleModel {
    public function update() {
        if ($this->price < 0) throw new Exception('Price cant be negative!');
    ...}
}

RedBeanPHP will connect the product bean $p with the model Model_Product in your domain model. You can also add new methods and call them using the bean:

$p->play(); //-- will call play() on Model_Product

Read more about Fuse:

http://www.redbeanphp.com/how_fuse_works

Hope this helps.

0
votes

You can do relationships with redbean.

$child->parent=$parentBean; // parent relationship
$child->ownToy=array($toybean1,$toybean2); // 1 to many relationship
$child->sharedToy=array($toybean1,$toybean2); // many to many relationship

So the question now is, how are you going to implement that? You can have something like:

var $relations=array(
    'fieldname'=>array('hasMany','names')
);

Then when you handle the save, just make sure you are calling the correct functions:

if(array_key_exists('fieldname',$this->relations)){
//magic happens
}

Hope that gets you pointed in the right direction.