active record class in codeigniter (Delete)
<?php
/**************** Deleting Data From Database ****************/
//Generates a delete SQL string and runs the query.
$this -> db -> delete('tablename', array('id' => $id));
//The first parameter is the table name,
//the second is the where clause.
//You can also use the where() or or_where() functions instead of passing the data
//to the second parameter of the function:
$this -> db -> where('id', $id);
$this -> db -> delete('tablename');
//An array of table names can be passed into delete()
//if you would like to delete data from more than 1 table.
$tables = array('table1', 'table2', 'table3');
$this -> db -> where('id', '5');
$this -> db -> delete($tables);
//If One wants to delete all data from a table,
//Then One can use the truncate() function, or empty_table().
$this -> db -> empty_table();
//Generates a delete SQL string and runs the query.
$this -> db -> empty_table('tablename');
//Generates a truncate SQL string and runs the query.
$this -> db -> from('tablename');
$this -> db -> truncate();
// OR
$this -> db -> truncate('tablename');
/************* Method Chaining **************************/
//Method chaining allows one to simplify syntax by connecting multiple functions
$this -> db -> select('title') -> from('mytable') -> where('id', $id) -> limit(10, 20);
$query = $this -> db -> get();
/*************** Active Record Caching **********************/
//While not "true" caching, Active Record enables you to save (or "cache")
//certain parts of your queries for reuse at a later point in your script's execution.
//Normally, when an Active Record call is completed, all stored information is
//reset for the next call.
//With caching, you can prevent this reset, and reuse information easily.
// Cached calls are cumulative.
// If you make 2 cached select() calls,
// And then 2 uncached select() calls,
// This will result in 4 select() calls.
//There are three Caching functions available:
//This function must be called to begin caching.
//All Active Record queries of the correct type
$this -> db -> start_cache();
//This function can be called to stop caching
$this -> db -> stop_cache();
//This function deletes all items from the Active Record cache.
$this -> db -> flush_cache();
//Generates: SELECT `field1` FROM (`tablename`)
$this -> db -> start_cache();
$this -> db -> select('field1');
$this -> db -> stop_cache();
$this -> db -> get('tablename');
//Generates: SELECT `field1`, `field2` FROM (`tablename`)
$this -> db -> select('field2');
$this -> db -> get('tablename');
//Generates: SELECT `field2` FROM (`tablename`)
$this -> db -> flush_cache();
$this -> db -> select('field2');
$this -> db -> get('tablename');
//Note: The following statements can be cached: select,
//from, join, where, like, group_by, having, order_by, set
?>
Comments
Post a Comment