Welcome Guest, Not a member yet? Register   Sign In
Advatage of "Active Record Database Pattern"
#1

[eluser]Unknown[/eluser]
Greetings.

While learning CI I stumble about the "Active Record Class" and for me - as a procedual oriented programmer I cannot see the real advantages of the "Active Record Database Pattern" clearly.

- OK, in an OO-Environment you have to use OO-Tools and OO-Syntax.
- OK, I understand that entries are automatically escaped

I read a little bit in Wikipedia and there I found out that the ARD-Pattern is also an abstraction-layer, and that the framework takes care of changing syntax in different SQL-Environments.

Was that all - or did I miss a "big thing" 8-)

Because, when I know that I will always use my sweet MySQL-Server and escape myself - it looks to me, that $this->db->query() will be faster to code. Or did I overlook something?
#2

[eluser]danmontgomery[/eluser]
I think it's most useful for people who have no database experience. IMHO it's main use is cleanliness/readability. It has it's benefits, but also severe limitations for complex queries. It can be nice for simple queries, eg:

Code:
$this->db->where('id', $id)->get($this->table);
// vs.
$this->db->query("SELECT * FROM `" . $this->table . "` WHERE `id` = " . $id);

It can also be useful for conditional updating, rather than a long string of concatenations you can just call $this->db->set() in if() blocks.

Code:
$this->db->set("field_a", "value_a");
if($a == true) {
    $this->db->set("field_b", "value_b");
}
// vs.
$this->db->query("UPDATE `some_table` SET `field_a` = 'value_a'" . ( $a ? ", `field_b` = 'value_b'" : '' ) . " ... ");

Or especially when updating/inserting a long list of fields.

The auto-escaping of fields fails in all but the simplest cases (I think the number of forums threads regarding this is proof enough), I generally have it disabled. There's no support for grouping where clauses, subqueries, etc. I don't really think it would be faster to just put queries in $this->db->query(), but I guess that depends on the nature of the application. If you're going to be spending more time fussing with incorrectly escaped queries and just passing it straight sql most of the time anyways, it may be faster.
#3

[eluser]omick[/eluser]
in an enterprise application which contains significant business logic and hence include lots of concurrently data accessed every now and then, we needed to find a way to simplify table access and integrate persistence with business logic and hence the Active Record Pattern... the active record pattern embeds the knowledge of how to interact with the database directly into the class performing the interaction(e.g.: on Models and Controllers) without extending the class or creating an instance every now and then.




Theme © iAndrew 2016 - Forum software by © MyBB