E Steven
E Steven

Reputation: 81

Kohana ORM Filters

I am trying to merge two values out of a form into a combined string using filters in Kohana, however I can't work out how to do it / if it is even the best way to do it or not.

I have two values, date and time in a form, which I want to convert to an epoch time string in the model and save that value into a datetime string in the database - what would be the best way of achieving this?

Upvotes: 0

Views: 2405

Answers (1)

matino
matino

Reputation: 17715

Filters are your best friend in this case. Below you can find example how to use them:

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

This filter will set your_column to strtotime($this->date . ' ' . $this->time) - of course this is just an example. Remember that filters are called before the validation.

EDIT:
After thinking through I'd create dedicated method for creating:

public function create_($values, $expected)
{
   $values['your_column'] = $values['date'] . ' ' . $values['time'];

   values($values, $expected);

   // Create the object
   return $this->create();
}

Of course when you call this method, don't pass date and time in $expected array but your_column:

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

Upvotes: 1

Related Questions