Yii Many_Many Relationship and Form

Posted by Thorpe Obazee on Stack Overflow See other posts from Stack Overflow or by Thorpe Obazee
Published on 2010-12-25T19:27:33Z Indexed on 2010/12/25 19:54 UTC
Read the original article Hit count: 170

Filed under:
// Post model
return array(
    'categories' => array(self::HAS_MANY, 'Category', 'posts_categories(post_id, category_id)')
);

I already have the setup of tables

posts
id, title, content

categories
id, name

posts_categories
post_id, category_i

The problem I have is that I am getting an error when creating this form:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'post-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'title'); ?>
        <?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'title'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'uri'); ?>
        <?php echo $form->textField($model,'uri',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'uri'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'content'); ?>
        <?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
        <?php echo $form->error($model,'content'); ?>
    </div>

    <div class="row">
        <?php // echo $form->labelEx($model,'content'); ?>
        <?php echo CHtml::activeDropDownList($model,'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'name')); ?>
        <?php // echo $form->error($model,'content'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

The error is:

Property "Post.category_id" is not defined.

I am quite confused. What should I be doing to correct this?

© Stack Overflow or respective owner

Related posts about yii