RedBeanPHP:如何添加时间戳,唯一和默认?

时间:2017-08-08 18:27:17

标签: php redbean

我使用的是最新的稳定版本,如果我需要更新版本5,请与我们联系。

<?php
$user = R::dispense('user');
$user->fname = 'man';
$user->lname = '00';
$user->setMeta("buildcommand.unique" , array($user->email='e@e.com'));
$user->pass = password_hash('password', PASSWORD_DEFAULT);

$user->gender=0; // I want this to have a default value of 0 if I don't insert anything
$user->verified=1;
$user->lastModified = date('Y-m-d G:i:s'); // I want this to be a timestamp, right now it's stored as datetime
$id= R::store($user);

lastModified存储为datetime。我希望genderverified的默认值为0.我希望email是唯一的。

我尝试了在stackoverflow上找到的所有解决方案,但没有任何作用

$user->setMeta("buildcommand.unique" , array(array('email')));

如何创建blob以及如何指定字段的长度?

1 个答案:

答案 0 :(得分:1)

要预设bean的值,您应该使用模型类的分配方法。见https://redbeanphp.com/index.php?p=/models

例如,要预设bean属性,您可以编写如下的模型类:

class Model_User extends RedBean_SimpleModel
{
    public function dispense()
    {
        $this->bean->gender = 0;
        $this->bean->verified = 1;
    }
}

要使电子邮件成为唯一属性,您应该使用RB元功能,而是在数据库中将该字段定义为唯一。

RB旨在让您在流畅的模式下处理业务逻辑,无论何时您的逻辑工作,您都可以选择冻结完整的数据库或部分表,如https://redbeanphp.com/index.php?p=/fluid_and_frozen所述。

如果RedBean在流体模式下无法猜测您想要的数据类型,建议您手动设置数据类型。