Wednesday, March 31, 2010

Posting with multiple models

I spent a good bit of time trying to figure out how to render multiple models in a form and have them each validate properly.

Knowing the solution, it seems quite obvious, but it took a while to track down the proper syntax.

In this example I have a custom CForm which contains a few different $model properties, in addition to the base form data.

To call the validation on multiple models in the form, simply modify any calls of
CActiveForm::validate($model);
to use an array of models as the first parameter as such:
CActiveForm::validate( array($model, $model->subModel, $model->subModel2) );

Friday, March 26, 2010

Themes for Mobile

Just an 'aha' or a 'well duh' moment (depending on how you look at it), but I realized today that I could set up a theme for 'mobile' friendly display.

So, I create my new mobile theme and adjust the views accordingly.

Then in my index.php I run a check for known mobile browsers against the $_SERVER['HTTP_USER_AGENT'] and if found, I set $config to /protected/config/mobile.php instead of main.php

Primary difference being the theme set, though I imagine I might add a few other differences over time.

Thursday, March 25, 2010

ActiveDataProvider w/ Scopes!

http://www.yiiframework.com/extension/active-data-provider-with-scopes/

Very easy to install!!

Scopes allow you to predefine filters for the record set and apply them and chain them without having to rewrite the conditions.
(Guide to named scopes)

i.e., in the model:
   public function scopes(){
       return array(
           'enabled'=>array(
               'condition'=>'isEnabled=1',
           ),
           'newest'=>array(
               'order'=>'dateAdded DESC',
               'limit'=>5 
           ),
       );
   }

This extension simply allows you to use the same functionality with the DataProviders.

Simply import the EActiveDataProvider from the extensions directory.
Then, in the $criteria, I simply add the scope and call EActiveDataProvider instead of CActiveDataProvider.

The REALLY nice thing about the scopes is that if the business logic needs to change at some point, I can do so at a single point of entry -vs- customized criteria scattered around.

Yii and Themes

In the face of a total site redesign today, I realized I should begin using the themes features of Yii. This is where some of the true beauty of MVC comes into play.

Without a custom theme set up, the theme will default to the views in the /protected/views/ directory. Once you set up a theme, whenever it cannot find a specific view in the theme directory, it will go back to the default in the /protected/views/ directory and check there.

This is the guide reference on themes: http://www.yiiframework.com/doc/guide/topics.theming

Thursday, March 18, 2010

Custom Behavior Example/Test

Ok, so I originally thought I should write a widget for this, then I realized that this was a good example of a behavior that should be attachable to multiple components.

I want to give the ability for the page to trim a string if it's over a given length, and return the truncated string with an ellipsis or other marker. A quick search through the yii documentation/forums did not find anything already built that would do this (though I did find one article in polish that had a function for doing truncation ;) Alas, I do not read Polish).

I'm adding this link to the list of items to read on behaviors in Yii:
http://www.yiiframework.com/doc/guide/extension.use#behavior

Events & Behaviors in Yii - Initial Notes

This one is a work in progress as I try to figure out how to use this aspect correctly. This are my "notes" from reading these documents.

I'm basing this on the following two document references:
http://www.yiiframework.com/doc/cookbook/44
http://www.yiiframework.com/doc/guide/basics.component (section on events)

Wednesday, March 17, 2010

Yii Authentication via database

Yii has a wonderful AuthManager built in, but it's a little confusing to use so many people end up creating their own. This one drove me a little nuts for a bit, so I thought it would be a good place to start.   For the purposes here, I'm going to assume you've already added a db setting to your main Controller. For more information on how to do that, read here.

This is the starting point for today's adventure: http://www.yiiframework.com/doc/guide/topics.auth

The article above walks you through adjusting the UserIdentity (found in /protected/components/UserIdentity.php ) to pull information from a database table for users, override the ID of the UserIdentity class and set up some basic authentication rules, but you have to read between the lines to get it all complete.

Exploring Yii

I've lately been exploring the Yii framework for PHP and I'm very much a fan of it. However, the documentation can be less than helpful most of the time. So, I've decided to start writing down my thoughts and observations as I go through it in the hopes that it will help others in similar situations.

Information on the framework can be found here: http://www.yiiframework.com/

I do not profess to be a Yii expert in the least, I'm a novice and these are novice observations to help others who also have trouble reading between the lines of the Yii documentation.  I may throw in some more advanced items in the future, but for now, it is what it is.

And off we go...