mahsa.teimourikia
mahsa.teimourikia

Reputation: 1774

Yii framework, after submiting the form text fields are not updated in database

I am pretty new with MVC and Yii framework, therefore there is a good chance that my question is pretty stupid. If so, accept my appologies in advance.

I want to have a form in my admin section of the website, where, user can post some content. I followed the tutorial for sample blog however it seems that the content added to the fields that are defined as text in the database (textarea in my form) don't get updated after submitting the form (everything else works fine). here is the sql statement for my table:

CREATE TABLE `tbl_show` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT,
   `presentation` text,
    PRIMARY KEY (`id`),
    KEY `fk_tbl_show_tbl_season1` (`tbl_season_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

I used Gii to make my Model and CRUD, I only add here the parts that might be related:

On my controller:

    public function actionCreate()
{
    $model=new Show;

    if(isset($_POST['Show']))
    {
        $model->attributes=$_POST['Show'];
        if($model->save())
                    {
                        $this->redirect(array('view','id'=>$model->id));
                    }   
    }
    $this->render('create',array(
        'model'=>$model,
    ));

and my _form.php:

<div class="form">

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

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

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

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

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

    </div><!-- form -->

Model Rules:

return array(
    array('id, presentation', 'safe', 'on'=>'search'),
);

I don't get why only the text content don't get updated, they appear as null in the database.

Thank you in advance, Cheers, Mahsa

Upvotes: 1

Views: 4367

Answers (3)

mahsa.teimourikia
mahsa.teimourikia

Reputation: 1774

I found the problem for the form not submitting correctly, I add it here in case anybody has the same problem:

I'd overwritten the beforeSave, but I wanted to complete it later so it wasn't returing anything:

protected function beforeSave() {
        if(parent::beforeSave())
        {
            if($this->isNewRecord)
            {
               //things that should be added by the system
            }
        }

    }

After commenting this everything works fine. Also as you guys recommended I added the fields that didn't have validation to the safe rule.

Upvotes: 0

Owais Iqbal
Owais Iqbal

Reputation: 549

try this ..

public function actionCreate()
{
    $model=new Show;

    if(isset($_POST['Show']))
    {
        $model->attributes=$_POST['Show'];

        $save = $model->save(false);      // write like this...
        if($save)
                    {
                        $this->redirect(array('view','id'=>$model->id));
                    }   
    }
    $this->render('create',array(
        'model'=>$model,
    ));

hope it will work for you..

Upvotes: -2

dInGd0nG
dInGd0nG

Reputation: 4114

You have to make the attribute safe . Add this to your Show models validation rules

array('presentation','safe')

Upvotes: 6

Related Questions