Tuesday, October 18, 2011

Learn to Love the Cache

I've avoided using the caching aspects of Yii for a long time, simply because I never had time to dig into it. My cursory understanding was that you needed to have APC or memcached enabled on your server for it to work, and that wasn't something I could guarantee. That understanding was NOT correct.

There are several caching options built in to Yii, including a simple CFileCache which will write files to your application/runtime/cache/ folder and a CDummyCache that you can use if you need to bypass the real cache without altering your code.

This example starts with just the most basic situation where we are consuming data from an external source and caching it locally so that we do not continually ping the remote server for data.

The Guide to Caching can be found here: http://www.yiiframework.com/doc/guide/1.1/en/caching.overview

Tuesday, August 9, 2011

Linked Required Fields

This came up today as something that is a very basic requirement when developing complex forms, but there was no way built in to Yii to handle it quickly and consistently across multiple models, so I created a custom validator for it. I hope it helps others to move forward quickly with their complex form development ;)


The need was that when a specific field was set or a specific value was selected for a field, additional fields would become required.

The solution? ChildrenRequiredValidator


http://www.yiiframework.com/extension/childrenrequiredvalidator/

Any feedback or suggestions are more than welcome!

Friday, August 5, 2011

Using GIT to control your production releases

In the process of looking up some documentation for work, I came across this article and I think it does a fantastic job of explaining a solid git development workflow and thought I should share it. Enjoy!!

http://nvie.com/posts/a-successful-git-branching-model/

Monday, August 1, 2011

Keeping your Yii Models Lean - Use Behaviors

Active Record models are fantastic for consolidating "black box" logic and keeping your models self-aware of their business logic, but what do you do when the business rules and object specific operations keep adding up?

Do yourself a favor, and get into the habit of putting those business logic methods into attachable behaviors.

Friday, June 3, 2011

Referencing views from alternate locations and themeability

This is something very simple, but it's very easy to do it 'wrong' and end up creating more work for yourself in the long run.

I ran into this again today when I was trying to create a theme for a site, and I knew that I want to be able to deploy the site in multiple locations (I will make another post later regarding distributed Yii applications) which employ different themes to present the same forms.

Thursday, March 31, 2011

Published EStrongPassword Extension

Not really much to it, but I thought it was something that might be useful for others.

http://www.yiiframework.com/extension/estrongpassword

This is an entirely client side solution. IMHO it should not replace solid validation on the server side as well.

On a side note, I've been all over the place with various projects and exploring the new features of Yii 1.1.7. I'm particularly looking forward to the business applications available with the new RESTful URL Manager options.

Friday, March 18, 2011

Using Scopes with DataProviders

I used to use an extension called EActiveDataProvider to get this functionality, but that broke with the release of 1.1.2 or 1.1.3, I can't quite recall. Suddenly, today, it dawned on me that I could easily get this functionality once more, by simply calling the scope function of the model and then passing in the particular scope I was looking for as the criteria parameter of the CActiveDataProvider.

For example...

In model Post, I add a scope as follows:
public function scopes()
{
   return array(
      'stats'=>array(
         'select'=>'post_id',
         'condition'=>'post_status=1', 
         'order'=>'post_date DESC',
       ) 
   );
}

Then, in my controller where I want to output just this set of data:
public function actionStats()
{
    $p = new Post();
    $scopes = $p->scopes();
    $criteria = $scopes['stats'];

    // Note: I could probably just Post::model()->stats()  but I haven't 
    // tested that.

    $dataProvider = new CActiveDataProvider( 'Post', array('criteria'=>$criteria));

    ... business as usual
}

Where previously I was doing something like this to be able to take advantage of the scope:
$dataProvider = new CArrayDataProvider( Post::model()->stats()->findAll() );

While the CArrayDataProvider is an acceptable alternative, I find that it does have some peculiarities when used with a CGridView, which the CActiveDataProvider avoids.

(Note: My scope is actually quite different and more complex, but I thought this made a clearer example).

EDIT:

Apparently, I need to read more about active finders, because you can actually do THIS as well:
$dataProvider = new CActiveDataProvider( Post::model()->stats() );

Wednesday, March 9, 2011

Published ESitemap Extension

The extension can be found here: Yii Extensions: ESitemap

This is an extension that provides class based actions which can be applied to any controller to generate a valid sitemap document. It comes with both a sitemap.xml option and a 'human readable' option.

You can configure any ActiveRecord class to be used as a source of site links, simply pass in the configuration options for the proper view of the object you wish to display.

I'm looking for feedback and ways to improve, so please share any comments/suggestions that you may have, either here, on the ESitemap Extension, or on the forum thread for the extension.

Hope it's helpful!

Saturday, January 22, 2011

Yii 1.1.6 and the beauty of yiic migrate

The new Migration feature is amazing. A-mazing.

In addition to migrating production servers from one version of your software to another, it's excellent for coordinating among a group of developers *AND* for coordinating between primary and test databases. Farewell SQL alter script coordination, it's been real ;)

If you haven't read it yet, I highly recommend reading this guide to migrating:

http://www.yiiframework.com/doc/guide/1.1/en/database.migration#creating-migrations

(Also, I recommend getting the bug-fix from the current yii trunk here: http://code.google.com/p/yii/source/detail?r=2907 which fixes the issue with the migrate not using the connectionId option properly.)

So, how do we actually apply this new wonderful feature? It's quite simple!