Teej
Teej

Reputation: 12873

Yii Many_Many Relationship and Form

// Post model
return array(
    'categories' => array(self::MANY_MANY, 'Category', 'categories_posts(post_id, category_id)')
);

I already have the setup of tables

posts
id, title, content

categories
id, name

categories_posts
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?

Upvotes: 1

Views: 7381

Answers (3)

npCoda
npCoda

Reputation: 13

Use in this way: In controller create object for category: $modelCat= new Category; then in

<?php echo CHtml::activeDropDownList(**$modelCat**,'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'name')); ?>

Upvotes: 1

thaddeusmt
thaddeusmt

Reputation: 15600

I use the CAdvancedArBehavior to make this easier. I wouldn't be surprised if something like this gets rolled into the Yii core eventually either:

http://www.yiiframework.com/extension/cadvancedarbehavior/

It lets you write code like this in your controller:

$model->categories = $_POST['Post']['categories'];

I use this with the more-bugggy-than-I-would-like "Relation" widget/extension. You might need to use that to get the POST variables to structure correctly, I'm not sure.

Upvotes: 3

Teej
Teej

Reputation: 12873

I honestly thought that Yii has something like Kohana or Cake in their AR anyway. I guess I have to manually add them.

foreach ($_POST['category_id'] as $category_id)
{
    $categoryPost = new CategoryPost;
    $categoryPost->category_id = $category_id;
    $categoryPost->post_id = $model->id;
    $categoryPost->save();
}

Upvotes: 2

Related Questions