At Tariff Street Ltd. we use Yii as our primary development framework. We chose the Yii framework for its excellent Active Record implementation, its strict PHP5 and OOP concepts, and because of our previous knowledge of Prado and ASP.NET, from which Yii borrows many ideas.
Ever been half way through a Yii development and thought “I wonder what version of yii am I using?”
Finding out is as simple as sticking the following link into your page…
Wonder no more as the currently used version will be right before your eyes!
One aspect of Yii that’s confused me in the past is where to put generic site-wide functions that you want to access throughout the application. Stuff like an encryption method or a hash generator, that don’t belong to a specific model.
The answer, of course, is to use Yii’s system of Extensions. Some reading through the documentation (http://www.yiiframework.com/doc/guide/1.1/en/extension.create) revealed that what I was looking for was an ‘Application component’ which extends Yii’s existing CApplicationComponent and is then available using
Firstly, create a new directory within protected/extensions called ‘functions’.
Then, in there, create a new php file called ‘Functions.php’
This file is your extension class, but it must extend CApplicationComponent:
class Functions extends CApplicationComponent
public function returnSomething()
Now go to protected/config/main.php and add the reference to your class in the ‘components’ array:
Now, all being well you can access your functions using:
Sometimes you want your validation rules to be just a little bit more than the standard options available to you like ‘numeric’, ‘length’ or ‘safe’.
Occasionally you might need a value to be unique within it’s database column:
array('username', 'unique', 'className' => 'User');
But sometimes you want unique to be more intelligent with it’s unique validation…
array('username', 'unique', 'className' => 'User', 'criteria' => array(
'condition' => 'active = 1',
The test above will only match when username is not unique AND active = 1. In the case of unique, criteria you define is appended to the existing check which would always take place.
Something I realised today is the power of adding conditions to model relations. This allows you to specify which records are returned as part of the relationship, which therefore allows you to set up several relationships for different situations.
'users' => array(self::MANY_MANY, 'User', 'user2project(project_id, user_id)', 'condition' => 'accepted = 1'),
This only brings back users who have accepted an invitation.
I can then also set up a new relation, called ‘invitedUsers’ where the condition is the opposite.
This allows me to access the users easily:
$project = Project::model()->findByPk(1);
$users = $project->users;
$invitedUsers = $project->invitedUsers;
The Alternative PHP Cache (APC) is commonly used with Yii applications, as it can speed up script running time by a factor of two. Compiled code is kept in shared memory for quick and easy access instead of being compiled each time a script is ran.
The only problem this can bring, is that if you a developing a site and APC is on, you won’t see any changes in your PHP code until APC is flushed.