0

任何人都知道可以将其转换为:

$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; 这样的语句。

非常感谢

4

1 回答 1

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);

祝你好运!

于 2013-04-18T13:41:00.807 回答