Yii Relational Active Record Tutorial

Yii website has an excellent written document on relational active record. I was confused at first with the example and some of the terms Yii used for their relational active record that really cause me to waste some time on this section. Therefore i decided to write a quick and dirty tutorial on Yii relational active record hoping it will post some benefits for those who are still learning Yii framework.

Yii Relational Active Record Approaches

In Yii there are two approaches mainly the lazy loading approach and the eager loading approach. Both approaches have been documented on Yii relational active record tutorial. But if you are lazy to read, this is how each approach is being use. Lazy loading approach is use when you are dealing with 1 record and eager loading approach comes in handy when there are more than 1 records you wish to access. This is made in this way to reduce the number of join which lead to inefficiency according to Yii document page

4 thoughts on “Yii Relational Active Record Tutorial

  1. Thanks for your tutorial; it's what I was looking for. However, I can't get the _view to put out the line items as indicated.
    This is my code for that file:

    beginWidget('CActiveForm', array(
    'id'=>'invoice-form',
    'enableAjaxValidation'=>false,
    ));
    $items=$model->invoiceItems;
    foreach($items as $item){
    echo $item->invoiceItemLineId . "";
    }
    ?>

    Fields with * are required.

    errorSummary($model); ?>

    labelEx($model,'invoiceId'); ?>
    textField($model,'invoiceId'); ?>
    error($model,'invoiceId'); ?>

    labelEx($model,'invoiceTotalAmount'); ?>
    textField($model,'invoiceTotalAmount'); ?>
    error($model,'invoiceTotalAmount'); ?>

    isNewRecord ? 'Create' : 'Save'); ?>

    endWidget(); ?>

    <!-- form -->

    Thanks again

  2. Sorry, the beginning of the file was clipped:

    beginWidget('CActiveForm', array(
    'id'=>'invoice-form',
    'enableAjaxValidation'=>false,
    ));
    $items=$model->invoiceItems;
    foreach($items as $item){
    echo $item->invoiceItemLineId . "";
    }
    ?>
    ....

  3. _view? i reckon it must have something to do with your controller not passing in the correct information to the view 🙂

Comments are closed.