Did you know FaceySpacey is the creator of Redux-First Router and Universal?

The Startup Incubator.
Your one stop social media shop
Resource Categories:

Technical
Non-Technical
Refer A Friend
Popular Articles
DEVELOPMENT TOOLS 1 - VERSION CONTROL (Mercurial)
CONTRACTING 2 - TOP 10 EASY SITE CREATOR TOOLS
Subscribe
MySQL 4 - MYSQL & Yii (PART 2)
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:
$articles = Article::model()->findAllByAttributes(array(‘type’=>’published’));
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.
Now let’s get into saving newly created articles:
$article = new Article; $article->title = ‘The FaceySpacey Programming Bible’; $article->pageCount = 300; $article->save();
See how natural that last line is. You can simply create an object, and call its save() method, rather than write this SQL to save it:
INSERT INTO article (title, pagecount) VALUES (‘The FaceySpacey Programming Bible’’, 300)
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.
Comments

SMM 3 - FORMULA TO FIND INFLUENCERS