Tariff Street Ltd and the Yii Framework

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.

Yii extensions – Where to put site-wide functions?

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

Yii::app()->yourExtension->yourMethod();

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:

<?php class Functions extends CApplicationComponent {     public function returnSomething()     {         return 'Something';     } } ?>

Now go to protected/config/main.php and add the reference to your class in the ‘components’ array:

'components'=>array(     'functions'=>array(                         'class'=>'application.extensions.functions.Functions',                 ),     ),

Now, all being well you can access your functions using:

<?php print Yii::app()->functions->returnSomething(); ?>

Adding Criteria To Validation Rules

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.

Adding conditions to Yii model relations

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.

for example:

'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;

Turning on APC per site

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.
Continue reading