Please enable JavaScript.
Coggle requires JavaScript to display documents.
W21 - Active Record Pattern (Applying Active Record with CodeIgniter…
W21 - Active Record Pattern
The Problem
Applications
boild down
to a lot of database operations; update, add, delete, retrieve
Active record pattern can simplify all of this
How it works?
A
domain model
in which the
classes
match very closely the
record structure
of a databse
Each active record is responsible for
saving/loading
database and also
for any domain logic
that acts on data.
The methods typically involved
Construct an instance of
Active Record from SQL result set row
Create new instance for
later insertion into table
Static finder methods
to wrap
commonly used SQL queries
and return
Active Record Objects
Update database from data in the Active Record
Get and set the fields
Implement some pieces of business logic
Drawbacks
Objects are
tightly coupled
to the database
Since pattern
is closely tied
to database, require
actual DB to test
Applying Active Record with CodeIgniter
Selecting data
$this->db->get('mytable')
.
$query = $this->db->get('mytable')
foreach ($query => result() as $row)
echo $row->title
$this->db->get_where('mytable', array(if=>if), $limit, $offset)
$this->db->select('title, content, date')
$this->db->get('mytable')
Inserting Data
$this -> db -> insert('mytable', $data)
$this->db->set('name', $name)
$this->db->insert('mytable');
Updating Data
$this->db->where('id', $id)
$this->db->update('mytable', $data)
Deleting Data
$this->db->delete('mytable', array('id' => $id));
.