[eluser]Unknown[/eluser]
Hullo,
I've been searching the forums on how to find a way to use the sql function NOW() with active records. I couldn't find a solution that I liked so I came up with this.
Background
The example illustrates a simple post in a blog or news feed. There is also a updated_at field which uses CURRENT_TIMESTAMP and the On Update use CURRENT_TIMESTAMP is set on the updated_at column. This is the reason we need the NOW() function in the query.
The alternative is of course to use PHP to get the current time in variable instead of using NOW(), but it feels much cleaner to have it in the query.
Solution
Code:
$data = array('header' => $header, 'body' => $body, 'author_id' => $author_id);
$this->db->set('created_at', 'NOW()', false);
$this->db->insert('news', $data);
This works great for me. But I wonder if there is any drawbacks or bad practice with this solution? Some other flaws maybe? I also wonder if this becomes one single query or if it is divided into two?
Anyways, enjoy the solution.