2

Pardon my ignorance on the matter but what's the point of using Query Builders? Isn't it far more succinct to write one line of SQL instead of 3 lines of AR code:

$this->db->query(" SELECT title, contents FROM data WHERE id = 2 ");

Instead of:

$this->db->select('title, contents');
$this->db->from('data');
$this->db->where('id', 2);

It just seems more verbose to me but then again I know nothing about Query Builders so I could be missing something. Would really like to know what the benefits are.

adlawson
  • 6,303
  • 1
  • 35
  • 46
Linda
  • 71
  • 3
  • Coming from someone who does not know what active records even is, it seems like the second method introduces less bugs because it isn't just one large string. Personally I like to read the second method a lot more. – Dominic K Aug 27 '11 at 19:36
  • 1
    Please be aware that this is **not** an ActiveRecord. CI guys misnamed it. See my answer here: http://stackoverflow.com/questions/2480929/is-codeigniters-activerecord-resource-intensive/2480944#2480944 - so the question should really be: what are the benefits of using a QueryBuilder? – Gordon Aug 27 '11 at 19:50
  • read the manual. AR takes care of more than just how much you have to type. http://codeigniter.com/user_guide/database/active_record.html – Peter Aug 28 '11 at 00:10

3 Answers3

2

If you need to programatically build your query CodeIgniter's Active Records are far more comfortable than plain text,

$id = 2;

//with CodeIgniter's Active Record
$this->db->select('title, contents');
$this->db->from('data');
if(isset($id))
   $this->db->where('id', $id);

//without CodeIgniter's Active Record
if(isset($id))
   $where = " WHERE id = {$id}";
$this->db->query(" SELECT title, contents FROM data".$where);

Ok, this isn't changing that much but what if you have 10 constraints on the where clause?

furthermore CodeIgniter's Active Record build the string in the right way (with placeholders ?) according to the data you pass i.e. you won't have to insert the ' manually on the query.

EDIT

@Col. Shrapnel said that there are no benefits with CodeIgniter's Active Record, since I'm not in agree with him I try to enforce my thesis with another example:

Let's do an example for an INSERT statement:

$array = array('A'=>'aaaa','B'=>'bbbb','C'=>'cccc');

//without CodeIgniter's Active Record
$query = "INSERT INTO my_table (";
$query.= implode(',',array_keys($array)) .')';
$query.= ......

//with CodeIgniter's Active Record
$this->db->insert('my_table',$array);
Dalen
  • 8,856
  • 4
  • 47
  • 52
  • for what I'm doing now it seems unnecessary but I read some of the other SO questions about Active Records and it seems valuable in the long run. – Linda Aug 27 '11 at 19:53
  • @Linda If you dont need to *dynamically* assemble queries, there is little benefit of using a QueryBuilder. You can just as well use an abstraction layer like PDO. – Gordon Aug 27 '11 at 19:56
  • in case of such a helper i would agree that a good programmer would create one for such a repetitive task. Anyway, for selects it only makes them hard to read. – Your Common Sense Aug 29 '11 at 15:25
2

I see no benefits at all.
So did Dalen say, "this isn't chanching that much". And with 10 constraint on the where clause AR just become even more werbose and messy, making you completely unable to grasp the meaning of the query. And there are no joins yet!

The only thing you really needed is support for placeholders.
With placeholders your queries become safe and easy to compose.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • actually placeholders are available also with $this->db->query there's no need of AR for using them. – Dalen Aug 29 '11 at 15:12
0

The style of programming you are complaining about should be impervious to SQL injection attacks. That's assuming that the DB interface you're talking to does sensible quoting and escaping, of course.

Warren Young
  • 40,875
  • 8
  • 85
  • 101