RedBeanPhp如何创建活动记录模型

时间:2013-03-08 10:15:01

标签: php redbean

任何人都知道可以转换它:

$u = R::dispense('users');
$u->login = $this->login;
$u->password = $this->password;
R::store($u);

进入这个:

class User
{
    var $login;
    var $password;
    var $id;

    public function save() {
        $u = R::dispense('users');
        $u->login = $this->login;
        $u->password = $this->password;
        $this->id = R::store($u);
    }
}

$u = new User();
$u->login = 'login';
$u->password = 'pass';
$u->save();

但没有使用代码保存这样的东西

public function save() {
    R::store($this);
}

我厌倦了重写诸如$ u-> login = $ this-> login;

之类的陈述 非常感谢

1 个答案:

答案 0 :(得分:6)

您应该创建一个扩展Redbean简单模型的用户模型,如下所示:

class Model_users extends \RedBean_SimpleModel
{
    private $_login = 'My default login name';
    private $_password = 'My default not so secret password';

    // The following method is executed right before an insert or update
    function update()
    {
        $this->bean->login = $this->_login;
        $this->bean->password = $this->_password;
    }
}

然后你可以简单地使用:

$user = R::dispense('users');
R::store($user);
祝你好运!