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!

Thursday, December 16, 2010

Published ES3 Extension

Just published an extension on the Yii framework site for interacting with Amazon S3. It's very nerve wreaking to share an Extension for some reason, so I've just never posted one before.

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

Pretty basic but quite handy if you use their services and need to push content to them. Hopefully this one will be useful!

Forum for questions/comments/concerns is here: http://www.yiiframework.com/forum/index.php?/topic/14265-extension-es3/


The extension is a simple Yii wrapper for the extremely effective S3 class created by Donovan Schönknecht which can be found here: http://undesigned.org.za/2007/10/22/amazon-s3-php-class

Tuesday, November 9, 2010

Posting Multiple of the Same model within a form

Recently, I was asked about posting multiple models of the same class within a form and how that can be accomplished.

The solution is to override the default name values for the form items to allow them to be presented in an array format, then in the controller, process each array item for the Class.

Lets say that I have a set of user bookmarks (Bookmark), and I want them to be able to update all the bookmarks from a single administrative page. This is a little rough, but it should explain the concept clearly:

Friday, October 29, 2010

Publishing to alternate assets directory

Sometimes, it's not possible to use the standard 'assets' directory for publishing assets. After struggling with this for a while, I discovered that the solution is, as is typical with Yii, even simpler than I could have hoped for.

Tuesday, September 7, 2010

Action paramater binding in Yii 1.1.4

The new Yii 1.1.4 contains numerous improvements. One that I wasn't clear on from the changelog was  the line saying "automatic action parameter binding" for $_GET values.  This is a very subtle way of saying that they've made it much easier to handle some of the 'grunt work' for actions that work with multiple $_GET variables.


http://www.yiiframework.com/doc/guide/basics.controller#action-parameter-binding

Quite simply pass in the name of the $_GET variables to the controller action method as parameters, including any default value you would like applied if they did not specify a value. 

Easily looked over, but very nice addition to the framework!!