You are currently browsing Crystal’s documentation for the 0.4 version - Switch to version: 0.3
Saving data
Inserting data
Inserting data in the database with Crystal is very straightforward.
// You supply an array with data
$data = array('title' => 'My title',
'body' => "Lorem ipsum body text");
$db->insert('table', $data)->execute();
last_insert_id()
Get the id generated in the last query
$data = array('title' => 'My title',
'body' => "Lorem ipsum body text");
$db->insert('table', $data)->execute()
$last_insert_id = $db->last_insert_id();
Updating data
// You supply an array with data
$data = array('title' => 'My title',
'body' => "Lorem ipsum body text");
$db->update('table', $data)->where('id','2')->execute();
Inserting and updating data actions in Crystal are not automatically executed with insert and update functions. Modifying and inserting data is crucial and because most database engines doesn't have UNDO action - there is no space for mistakes. That's why Crystal have the additional execute function, so you can test your data integrity or debug your query, before executing INSERT or UPDATE actions.