Now let me explain with an example:
if I want to get everything in a table of users I will code it like this with php and normal sql:
//construct the sql
$sql="select * from users order by dateregistered desc";
//execute the query ingnoring the errors
$result=@mysql_query($sql);
/// now fetch
while($record=mysql_fetch_assoc($result)){
echo "username: ".$record['name']."
";
}
Thats realy boring imagine handling all the errors everytime you perform a query. I can't remember the last time I wrote codes like that. Imagine coding a solution as big as twitter and u have to write such codes.
Now lets do it with active records
//instantiate the active records database class
$db=new activeRecordsDB(array('host'=>'localhost','username'=>'root','password=>'passwod'));
//order the query by date registered
$db->orderby('dateregistered','desc');
//execute the query
$records=$db->get('users');
//loop through the records
foreach($records->result() as $user){
echo "name: ". $user->name;
}
This is how you add WHERE, LIKE condition statements using active records
/instantiate the active records database class
$db=new activeRecordsDB(array('host'=>'localhost','username'=>'root','password=>'passwod'));
//add a where clause
$db->where('name','%churchill%');
//or add a like clause
$db->like('website','%afrovisiongroup.com%');
//order the query by date registered
$db->orderby('dateregistered','desc');
//execute the query
$records=$db->get('users');
//loop through the records
foreach($records->result() as $user){
echo "name: ". $user->name;
}
With active records we have more readable codes and easy to understand and work on with other developers in a team. Nowadays I work with projects with guys in different geographical locations, with active records our codes are easy to comprehend.
There are more advanced implementations of active records in Java, .NET, PHP , ruby on rails , etc. but what i wrote here is just an understanding of how active records makes web development very very fast and smooth.
Active records information can be read from wikipedia following this link http://en.wikipedia.org/wiki/Active_record
No comments:
Post a Comment