[SOLVED] Blog posts actionCreate is creating 6 copies

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.

I am pulling my hair out over this. If anyone has any helpful hints or info please help me save my hair :)

I don’t remember the structure of the blog demo but I think you should try to isolate the problem to server (file logging) or client side (Firebug).

/Tommy

Tommy,

I am an idiot for not using firebug to debug this earlier. I figured it had to be something on the server end so I was just doing a crap load of PHP debugging. Turns out, I had Ajax Validation set to ‘true’ on the form and that was causing the form to be submitted every time I moved to the next field on the form ( 6 fields meant 6 copies of the post ). As soon as I turned on FireBug I saw it performing the ajax requests and figured it out.

Can’t thank you enough!