Yii Framework: Retrieve scoped Relations of a Model

With Relational ActiveRecord implementations, it is very common to retrieve records from relationships:

$criteria = new CDbCriteria();
$criteria->condition = 't.slug=:slug';
$criteria->params = array(':slug'=>$_GET['slug']);

$category = Category::model()->find($criteria);

// Retrieve Deals of a Category
$deals = $category->deals //array

Scoped Relations

Sometimes you want to retrieve relations that have a scope . In the Yii Framework, you can do the following:

$criteria = new CDbCriteria();
$criteria->condition = 't.slug=:slug';
$criteria->params = array(':slug'=>$_GET['slug']);

$category = Category::model()->find($criteria);

$criteria = new CDbCriteria();
$criteria->condition = 't.category_id=:category_id';
$criteria->params = array(':category_id'=>$category->id);

// Retrieve Deals with a 'published' scope
$deals = Deal::model()->published()->findAll();

A Better Way

$criteria = new CDbCriteria();
$criteria->condition = 't.slug=:slug';
$criteria->params = array(':slug'=>$_GET['slug']);

$category = Category::model()->find($criteria);

// Retrieve Deals with a 'published' scope
$deals = $category->deals(array('scopes' => array('published')));

Awesome!

Yii Framework: Passing Parameters to Widgets

The widget is fun way for Yii to create logic into views. To pass parameters to a widget, you would add them as properties to the widget class.

Widget

class MainMenuWidget extends CWidget
{
    // A property
    public $title = '';

    public function run()
    {
        $categories = Category::model()->findAll();
        $this->render('mainMenuWidget', array('menu_items'  => $categories));
    }
}

View

You need only pass an array as the second parameter of $this->widget.

$this->widget('MainMenuWidget', array('title' => 'my page title');

Nice huh!

Yii Framework: Populating the CActiveForm Dropdownlist

Controller Method

In the method you need to use, send the records from the model to the view.

class DealsController extends Controller
{
    public function actionCreate()
    {
        $model=new Deal;
        $this->render('create', array(
            'model'=>$model,
            'categories' => Category::model()->findAll()
        ));
    }
}

View

We will be using CHtml::listData for formatting data to be usable for the dropDownList method:

<div class="form-group">
    <?php echo $form->labelEx($model,'category_id'); ?>
    <?php echo $form->dropDownList($model,'category_id', CHtml::listData($categories, 'id', 'name'), array('class' => 'form-control')); ?>
    <?php echo $form->error($model,'category_id'); ?>
</div>

Yii Framework: Bootstrap the Validation ErrorSummary

A ton of people are now using Bootstrap for prototyping and also for production sites. In Yii Framework, most often than not, you’d be getting your hands dirty customizing output.

The errorSummary() is the Yii Framework’s way to output errors.

Method 1

<?php echo $form->errorSummary($order, '<strong>Please fix the errors on the form:</strong><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>', '', array('class' => 'alert alert-danger')); ?>

Method 2

<?php echo $form->errorSummary($order, '<strong>Please fix the errors on the form:</strong><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><div class="alert alert-danger">', '</div>'); ?>

Both the methods would produce:

<div class="alert alert-danger">
  <strong>Please fix the errors on the form:</strong>
  <button type="button" class="close" data-dismiss="alert">
    <span aria-hidden="true">×</span>
    <span class="sr-only">Close</span>
  </button>
  <ul>
    <li>First Name cannot be blank.</li>
    <li>Last Name cannot be blank.</li>
    <li>Street cannot be blank.</li>
    <li>Province State cannot be blank.</li>
    <li>City cannot be blank.</li>
  </ul>
</div>