I have been creating a test app with Yii to get to know the framework. I basically followed the blog demo and then added a few extras like blog categories and post thumbnails. Everything was working great but now when I create a new blog post, 6 copies of the post are entered into the Db. It is very odd that it is happening but even more odd is that 3 of the posts are set to ‘published’ and 3 are set to ‘draft’. I have looked at my relations and simplified the actionCreate() function down to the basics but can’t figure it out.
PostController.php actionCreate
public function actionCreate() {
$model=new Post;
if(isset($_POST['Post'])) {
$model->attributes=$_POST['Post'];
if ($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array('model'=>$model));
}
My relations look like this:
Post Model Relations:
return array(
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
'comments' => array(self::HAS_MANY, 'Comment', 'post_id', 'condition'=>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'),
'commentCount' => array(self::STAT, 'Comment', 'post_id', 'condition'=>'status='.Comment::STATUS_APPROVED),
'category' => array(self::BELONGS_TO, 'postcategories', 'category_id'),
);
Category model Relations:
return array(
'posts' => array(self::HAS_MANY, 'Post', 'id'),
'postCount' => array(self::STAT, 'Post', 'category_id', 'condition'=>'status='.Post::STATUS_PUBLISHED),
);
I am also using the Relation component to spit out a category select box in the Post/_form.php view:
$this->widget('application.components.Relation', array(
'model' => $model,
'relation' => 'category',
'showAddButton' => false,
'fields' => 'title'
));
I am very new to setting up the relations so I think I have them right but I am not sure.
Any help would be greatly appreciated. If it helps, there are 8 categories in the database but the post is being inserted 6 times.