• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active record vs SQL

#1
[eluser]Dave Stewart[/eluser]
HI there,
I'm pretty new to CI so willing to take on all opinions for a new way of working.

However, I can't really see the advantage of using Active Record over SQL for a lot of queries. I'm pretty handy with SQL, so can someone explain what I can't see?

In many cases the SQL is just easier to read in my view, and I would imagine certainly easier to debug, as you just dump it into phpMyAdmin or such like if your query fails.

Saying that, I haven't used AR yet, so I don't know.

So in what situation would I use the AR methods, and in what situations would I just use query($some_sql)?

Thanks,
Dave

#2
[eluser]xwero[/eluser]
It's up to you. The query method has bindings to escape the values too so that is not the reason.

i think the main reason to use AR, for me at least, is to write less code
Code:
$query = $this->db->query('SELECT field1, field2 FROM table');
// VS
$query = $this->db->select('field1, field2')->get('table');
The AR methods are more readable for some and they encourage writing safer sql statements. You can also cache portions of the sql statement which means even less code.
Because you build the sql statement with methods it's easier to add or remove a portion. With the query method you have to build the sql statement without any help.

Whatever you choose try to be as consistent as possible. The query method can handle all the queries but AR can't because there were decisions to have the methods available for different databases. Not all methods are supported by all databases but most are.

#3
[eluser]Dave Stewart[/eluser]
Thanks for the reply.
I'll check it out as I develop my new application.

Cheers,
Dave

#4
[eluser]nanda[/eluser]
I used to also prefer writing my own sql but use mostly active records now. The main advantages for me are that active records are not database specific, so if I change from MySQL to something else in the future my queries should be fine, also I'm finding the active query method less messy and simple to read & edit.

If you want to debug your queries from an active record you just need to turn on profiling in your controller ($this->output->enable_profiler(TRUE)Wink, all queries run on a page are shown at the bottom of the page and you can copy and paste away.

#5
[eluser]Scriptor[/eluser]
I started with AR since I wasn't completely comfortable with SQL. It definitely seemed to be easier than just writing raw SQL. Also, the cacheability and DB-independence are bonuses mentioned before are plusses. On the other hand, if you're comfortable with SQL and databases in general, then you should go ahead and forego AR. This is probably one reason why CI supports both ways of doing things.
Also, besides enabling profiling,
Code:
$this->db->last_query();
is also helpful when you want to output the query used. I usually use it anyway since the query in my actual code could have variables are ?'s (for binding) in it.

#6
[eluser]Eric Cope[/eluser]
I use the Active Record method because it auto-escapes my input data, it is easier for me to read, and it remembers how to word the SQL statement (I forget because I actively use too many languages). As mentioned above, it is also database-independent.

I am not an expert at SQL, so its easier for me to use AR. If you are an expert at SQL, then I would recommend using SQL directly. Thats the beauty of CodeIgniter, it does not force you to use one method or another, it gives you options and choices.

#7
[eluser]Dave Stewart[/eluser]
Thanks for the info folks Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.