I have done many CodeIgniter projects and in that i have only used $this->db->query and not any other methods.
People say that using this will complicate when we change the database.
So people use db->get, db->where and functions like that including join.
For example i am using mysql now with $this->db->query through out my project and if i am changing my database to any others … will it be a problem…
Besides we should be very clear of selecting the database before starting the project and this will be a good practice and people know about this… besides… this…
what will be the discrepancy if we only use $this->db->query in a CodeIgniter Project ?
Any type of answer would be helpful… advices, suggestions and answers. 🙂
In some ways I see a benefit to using the Active Record classes, but in other ways it can be a deficit. Let me elaborate.
As birderic said, the benefit of using the Active Record class is that CodeIgniter basically writes the t-SQL statements for you in the database engine that you have specified in your config.php file. Take for example the following code:
Would generate this in MySQL:
SELECT * FROM Users WHERE Name = 'bob' LIMIT 0, 10.And this in PostgreSQL:
SELECT * FROM Users WHERE Name = 'bob' LIMIT 0 OFFSET 10The slight differences in the tSQL are taken care of by the Active Record class and it’s query builder. This is a huge benefit when you are either migrating your database, or testing your model code on a new datasource (maybe for unit testing with a SQLite database for instance).
However, on the flip-side of the coin I think that typically databases tend to be more difficult to change and have a lot more “catches” when migrating. If you write all your code as Active Record code, then it is effectively forever tied to CodeIgniter. It is quite difficult to then shift that code and those queries to a new MVC framework, or even a new language down the road.
Using the Active Record class gives you database freedom, but ties you down to CodeIgniter. Using normal SQL queries gives you framework freedom, but ties you down to your database.
That said, I find a mixture of both works well. Sometimes my queries are quite complex and have sub-queries, all sorts of joins, weird SQL Server specific syntax … so then I have to use $this->db->query(). However, for all the simple stuff I use Active Record.