Welcome Guest, Not a member yet? Register   Sign In
Is it just me, or do Helpers and Active Record add more work than they save?
#1

[eluser]TheActionCombo[/eluser]
First, I'm kind of new to CI so please set me straight if I'm not understanding something correctly.

I've been looking though many of the Helpers as well as the Active Record class in the user guide, and it seems like they don't actually speed up development, but actually slow it down.

Looking at certain HTML Helpers such as the URL helper and Form helper make me wonder "What's the point?" Using the helpers not only requires you to learn how to use them, but they're pretty much much the same amount of code as doing it the "long way". That said, a lot of the non-HTML helpers do seem pretty useful.

Active Record also perplexes me. Why write multiple lines of code defining each part of an SQL query when you can just write out the query on one line? For example (taken from the User Guide):

$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);

// WHERE = 'Joe' AND title = 'boss' AND status = 'active'

Can someone explain why/when I would want to use Active Record over simply writing out the SQL query?

Just trying to understand certain parts of the framework better.

Thanks.
#2

[eluser]Rick Jolly[/eluser]
For the most part, I agree with you about helpers.

As for Active Record, to be fair, the equivalent query without AR would look more like this:
Code:
$sql = "...WHERE name = ? AND title = ? AND status = ?";
$this->db->query($sql, array($name, $title, $status));
#3

[eluser]Référencement Google[/eluser]
I personally do like that, I feel things are more clearer than repeating the where statement:

Code:
$where = array('page_ID'  => $page_ID,
               'language' => $language,
               'active'   => 1);
        
$query = $this->db->getwhere('cme_pages', $where);
#4

[eluser]schnoodles[/eluser]
Also remember if you use Active Record you can change what database driver you want to use, and all those queries will still work without changing a thing.
#5

[eluser]Référencement Google[/eluser]
[quote author="schnoodles" date="1194092924"]Also remember if you use Active Record you can change what database driver you want to use, and all those queries will still work without changing a thing.[/quote]

I think that's not right. It's not meaning use of Active record, it is mean use of the Database class, and while he will use $this->db to make his query's he get the benefits of all the DB abstraction layer, including the driver abstraction.
#6

[eluser]esra[/eluser]
I believe that Martin Fowler first described the Active Record design pattern and it has been implemented for many database abstraction layers and database engines using different approaches which can vary considerably based on Fowler's original description and implementations for specific programming languages and/or frameworks. The general idea is to represent a database as closely as possibly to programmable objects and then manipulate and nest those objects in an object-oriented manner.

There is some confusion about what is Active Record and what is ORM. True Object Relational Mapping (ORM) solutions are usually built upon an Active Record layer. Some Active Record implementations (e.g., ADODB) refer to their Active Record implementation as an ORM to add to the confusion. Some design pattern books (PHP|Architect's Guide to PHP Design Patterns, Jason Sweat) and various web resources refer to the ADODB implementation as a different design pattern (Table Data Gateway pattern). CodeIgniter's Active Record syntax was based on ADODB.

Support for transactions is usually based on Active Record or a related design pattern.

Active Record is like anything else in most frameworks--you can use it if you want to and ignore the it if it is not wanted. The same could be said of the HTML helpers or any other non-core extensions within the framework. By non-core, I'm referring to any framework extension not loaded by CodeIgniter.php.
#7

[eluser]TheActionCombo[/eluser]
So what would be the advantage of using Active Record in CodeIgniter over simply writing out the SQL queries by hand? From what I can see, Active Record doesn't make things easier or faster, it's just a different way of doing it (which in my opinion is a bad thing since it requires you to first learn the Active Record class). Am I missing something here?
#8

[eluser]thurting[/eluser]
I think it is mostly for people who are unfamiliar with SQL or who are too lazy to write out the SQL. The use of the term "Active Record" is quite misleading because that phrase has a specific meaning in computer science. CI's "Active Record" should really be called SQL Helper. I agree with you... if you are comfortable with SQL then write it out yourself.
#9

[eluser]esra[/eluser]
[quote author="TheActionCombo" date="1194124408"]So what would be the advantage of using Active Record in CodeIgniter over simply writing out the SQL queries by hand? From what I can see, Active Record doesn't make things easier or faster, it's just a different way of doing it (which in my opinion is a bad thing since it requires you to first learn the Active Record class). Am I missing something here?[/quote]

Absolutely nothing. Like I said, it is an option you have if you ever want to use it. For my personal projects, I always write SQL and use ADODB (because I'm accustomed to using it and because it has proven to be stable and consistent over the years). At work, we use Doctrine's DBAL with SQL queries (don't have a need for the Active Record, Transactions or ORM plugins).
#10

[eluser]Phil Sturgeon[/eluser]
[quote author="TheActionCombo" date="1194124408"]So what would be the advantage of using Active Record in CodeIgniter over simply writing out the SQL queries by hand? From what I can see, Active Record doesn't make things easier or faster, it's just a different way of doing it (which in my opinion is a bad thing since it requires you to first learn the Active Record class). Am I missing something here?[/quote]

I find it helpful, mostly because it handles my arrays for me. If I have a data array passed to a function that I want to insert, then $this->db->insert('Table', $data); is a crapload easier than the optional foreach string concatanation.

CI's ActiveRecord has no real benefit over simple selects, but I find not only in situations like above, but in joins it can really simplify things too. Its much easier to follow the syntax for joins as it breaks down the queries in a more logical way.




Theme © iAndrew 2016 - Forum software by © MyBB