CActiveForm (on child object) with lazy loading

Ok, so people may have noticed a common thread as I’m trying to learn Yii’s lazy loading stuff… :)

Anyway, so here I have a CActiveRecord with:





    public function relations() {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'createdBy' => array(self::BELONGS_TO, 'User', 'created_by'),

            'parent' => array(self::BELONGS_TO, 'Page', 'parent_id'),

            'children' => array(self::HAS_MANY, 'Page', 'parent_id'),

            'myContent' => array(self::HAS_ONE, 'PageContent', 'page_id'),

            'pageOrders' => array(self::HAS_MANY, 'PageOrder', 'page_id'),

        );

    }




And in my form I have:





    <div class="row">

        <?php echo $form->labelEx($model, 'title'); ?>

        <?php echo $form->textField($model, 'title'); ?>

        <?php echo $form->error($model, 'title'); ?>

    </div>


    <div class="row">

        <?php echo $form->labelEx($model, 'description'); ?>

        <?php echo $form->textArea($model, 'description', array('rows' => 6, 'cols' => 50)); ?>

        <?php echo $form->error($model, 'description'); ?>

    </div>


/* Below is the problem area. */


    <div class="row">

        <?php echo $form->labelEx($model->myContent, 'content'); ?>

        <?php echo $form->textArea($model->myContent, 'content', array('rows' => 6, 'cols' => 50)); ?>

        <?php echo $form->error($model->myContent, 'content'); ?>

    </div>




/* ************ */


    <div class="row">

        <?php echo $form->labelEx($model, 'created_by'); ?>

        <?php echo $form->dropDownList($model, "created_by", CHtml::listData(User::model()->findAll(), "id", "name")) ?>

        <?php echo $form->error($model, 'created_by'); ?>

        

    </div>



When loading the form with a model that has a PageContent object assigned to it, everything works fine. Form renders and the "content" area of the form is populated from the myContent child. I can even save the changes made to it and everything.

But when I load the form up for an empty model, the myContent lines die with the error:


Fatal error: Call to a member function isAttributeRequired() on a non-object in /var/www/yii-1.1.9.r3527/framework/web/helpers/CHtml.php on line 1185

After some poking through Yii’s code, it appears that somehow, when $model is an empty CActiveRecord, the $model->myContent is a CHtml object as far as I can tell but when $model is not empty, $model->myContent is the proper object.

My goal? Pretty much like a forum type of deal. There’s “pages” and every page is assigned a “page_content” object (In the future, each page will have a revision history so each page will have multiple page_content objects but for now trying to get it to work with one child). So I want the “edit” feature to edit the page’s information as well as the page_content data.

Helps???

If I understand correctly, when you create an empty parent CActiveRecord, you want the relations to also be instantiated with their respective empty CActiveRecords? I was having the same problem, but I solved it by adding this to the parent CActiveRecord:




public function init()

{

	if($this->isNewRecord)

	{

		$this->myContent=new PageContent;

	}

}



I’m also new to Yii, so I’m not sure if this is the correct way, or if there’s a built-in way to accomplish this.