PHP Yii and Ruby on Rails Active Record

I have been using the Yii Framework for over two years now and have been using the Ruby on Rails framework for around the same time period. I have done various projects in each and thought I would share my experience. The scope of this post will just be the differences between active records. It will not go into other details about the two frameworks. The purpose of this post is also not to debate which one is better. The purpose is to simply give the differences between the two and show the capabilities of each. This will only cover some features of each it will not include all of the features.

Ruby on Rails

Ruby on Rails has a powerful active record implementation. For example if you want to find something by it’s primary key. You would write something like Model.find(:id). This would then return an active record object with the properties of that single item. You can also add multiple conditions by simply by calling the where method. It would look similar to this Model.where(id: 10, text: ‘whatever’). This will then return a set of active record objects instead of just the single object the find would return. If you know there will only be one result you can simply do .first to get the result back.

Another part of active record is scopes. You can create scopes within a model and call them directly instead of having to write the where conditions over again. So if you need to write a query multiple times it’s a sign that you should create a scope instead. A scope is just defined in the model and can use lambda to pass in dynamic parameters.

Associations is a big part of Rails or any framework for that matter. In Rails associations are very simply and if it’s a simple relationship is dead simple. For example a post can have many comments. At the top of your post model you would add in a has_many :comments. As long as the keys match up that’s all you have to do. You can also do more advanced relationship mappings to.


Yii has a powerful active record implementation also but uses a different implementation method. For example if you want to find a record by it’s primary key you can simply do ModelName::model()->findByPk(‘key’);. That will return a single active record object back. If you need to do more advanced queries you can do it in two ways. The first is to call the findByAttributes method. That would looks something like ModelName::model()->findByAttributes(array(‘a’ => ‘test’, ‘b’ => ‘whatever’));. That implementation will also just return a single active record object back if found. For advanced queries you can also create a CDbCriteria object with the criteria generated. When you look at the documentation you will see the various attributes you can set. After setting the object you can then simply call your query like this ModelName::model()->find(CDbCritieriaObject);.

Yii also has support for scopes. Similar to Rails Yii scopes are also defined in that specific model. A scope is Yii is basically a short cut to creating the CDbCriteria object. You will be passing in very similar parameters. It’s just as powerful as CDbCriteria also so you can do advanced queries without much of an issue.

Associations are also a big part of the Yii framework. They take a little more code than the Rails framework though. For example to do a has_many relationship you have to define it like this.

public function relations() {

return array('relationshipName' => array(

self::HAS_MANY, 'RelationshipModelName', 'RelationshipModelNamePk'





As you can see it takes a little bit more code to define them. These can be auto generated though through the Gii tool. So they are not as big of a deal to code up as 99% of the work is done if not 100%.

Those are some of the difference between the two of them. There are a lot more differences and this only covers the surface. The goal of this post is really to just share that each can do very similar things just in different ways. If you end goal is to get something up and running I feel that both frameworks can handle that. It comes down to a preference between Ruby and PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *