Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Pure SQL, Yii Active Record, or Mixed

Trying to sort through the advantages or disadvantages of the different approaches. Really not looking for opinions, rather what these different approaches provide or limit.

Yii claims that using their AR simplifies the DB programming but I am more concerned with "meshing" the DB with my code too much. I obviously know they work together, but the mapping is a bit of a concern. I want as many controls and constraints to exist withing the DB and wonder if getting away from pure SQL might limit performance in anyway.

like image 722
enfield Avatar asked Feb 25 '23 17:02


1 Answers

ActiveRecord is good for any system with pretty straight-forward ORM where Model == Table, with a 1-to-1 relationship. In other words, if all of your data objects fit in single tables (perhaps with a few extra tables relating them) ActiveRecord is for you. And you can usually design your software so this is the case!

But you are correct, it does "mesh" the DB implementation and code a bit. It is assumed that Model == Table, meaning that the database implementation is NOT separate from your code. ActiveRecord breaks down a bit if you have complex Models stored in multiple tables. But that is a rather involved argument I won't get in to. Other patterns like Data Mapper give you more flexibility, but they require more work to code initially.

ActiveRecords is set-it-and-forget-it. SO easy and fast. No tedious coding of getters and setters, just nice PHP objects built from your tables. I built a pretty large app with Yii recently and I love the speed and simplicity of ActiveRecord. I am working on an app in Zend now, and even though I can see how their way gives you more control I miss the ease of AR. ;)

like image 116
thaddeusmt Avatar answered Mar 11 '23 08:03