Kohana ORM Filters


Kohana ORM Filters

我正试图将两个值从一个表单合并到使用Kohana过滤器的组合字符串中,但是我无法弄清楚如何做到这一点/如果它甚至是最好的方法来做到这一点。

我有两个值,日期和时间的形式,我想在模型中转换为一个epoch时间字符串,并将该值保存到数据库中的datetime字符串-实现这一点的最佳方式是什么?

在这种情况下,

过滤器是您最好的朋友。下面你可以找到如何使用它们的例子:

public function filters()
{
    return array(
        // Trim all fields
        TRUE => array(
            array('trim'),
        ),
        // Your callback function
        'your_column' => array(
            array(array($this, 'your_callback'))
        ),
    );
}  
public function your_callback($value)
{
    return strtotime($this->date . ' ' . $this->time);
}

此过滤器将your_column设置为strtotime($this->date . ' ' . $this->time) -当然这只是一个示例。记住,过滤器是在验证之前调用的。

编辑:


经过思考,我创建了一个专门的方法来创建:

public function create_($values, $expected)
{
   $values['your_column'] = $values['date'] . ' ' . $values['time'];
   values($values, $expected);
   // Create the object
   return $this->create();
}

当然,当你调用这个方法时,不要在$expected数组中传递datetime,而要传递your_column:

ORM::factory('event')->create_($_POST, array('title','venue','your_column', 'guest_count', 'contact_name', 'contact_number','bartab','dj'));