Now we’re going to inspect simpler ways of finding and saving data that Yii models have built into them. Yii models fyi are based on the “Active Record” pattern. Look it up some time: http://en.wikipedia.org/wiki/Active_record_pattern . All you need to know for now is the reason that model classes extend from a class called CActiveRecord is because Yii uses concepts from the Active Record pattern to design how your models will work.
So anyway the CActiveRecord parent class is what gives your models methods like findByPk() and findByAttributes() that you’re now familiar with. Let’s go back to an example similar to what you’ve seen before in previous tutorials:
So that code is obviously finder code to return a list of published articles. What’s going on here is that in placement of SQL code such as this “ SELECT * FROM article WHERE type = ‘published’ ” the findAllByAttributes() method is used. And of course that method takes as a parameter an array with data about what to search for. The idea is that this is native PHP coding, and no SQL is required. It will make coding a lot easier and save you a lot of time.
The Active Record tools of Yii allow you to think in terms of PHP code, and more specifically in terms of Yii code, rather than Yii code, PHP mysql functions, and Mysql. It allows you to not have to jump back and forth between different languages, and think in terms of just a few core method and the parameters you pass to them. Basically it means there’s a lot less to remember, and what you do have to remember is the most powerful stuff that offers the most flexibility. In reality, in terms of finder methods such as findAllByAttributes(), it will all be about the parameters you pass to it. And these parameters are usually the application-specific pieces of information that are easy to remember such as table column names since it’s your application, rather than tons of coding syntax.