Model Not Saving, No Validation Errors

I have a model that, for some reason, is not saving to the DB.

$model->save() returns false, but $model->validate() returns true, and $model->getErrors() returns empty.

I tried making it print all the attributes so that I can enter them into the MySQL table manually (you can see in the beforeSave() method below), and it inserted fine.


<?php


/**

 * This is the model class for table "comment".

 *

 * The followings are the available columns in table 'comment':

 * @property string $id

 * @property string $user_id

 * @property string $content_id

 * @property string $title

 * @property string $body

 * @property string $created

 * @property string $updated

 *

 * The followings are the available model relations:

 * @property User $author

 * @property Content $content

 */

class Comment extends CActiveRecord

{

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


 function tableName()

    {

        return 'comment';

    }


    public function rules()

    {

        return array(

            // array('user_id, content_id, created, updated', 'required'),

            array('content_id,body', 'required'),

            array('user_id, content_id', 'length', 'max' => 10),

            array('title', 'length', 'max' => 255),

            array('body,title', 'safe'),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id, user_id, content_id, title, body, created, updated', 'safe', 'on' => 'search'),

        );

    }


    public function relations()

    {

        return array(

            'content' => array(self::BELONGS_TO, 'Content', 'content_id'),

            'author' => array(self::BELONGS_TO, 'User', 'user_id'),

        );

    }


    public function attributeLabels()

    {

        return array(

            'id' => 'ID',

            'user_id' => 'User',

            'content_id' => 'Content',

            'title' => 'Title',

            'body' => 'Body',

            'created' => 'Created',

            'updated' => 'Updated',

        );

    }


...


    public function beforeSave()

    {

        $this->updated = gmdate('Y-m-d H:i:s');


        if ($this->isNewRecord)

        {

            $this->created = $this->updated;

            $this->user_id = Yii::app()->user->getId();

        }

        /*

        echo '<pre>';

        foreach ($this->attributes as $attribute => $value)

            echo $attribute . ': ' . $value . '<br />';

        // die();

        */

        parent::beforeSave();

    }


}

Controller:




<?php


class CommentController extends Controller

{

	public function actionCreate($id)

    {

        $model = new Comment;

        $model->content_id = $id;


        // Uncomment the following line if AJAX validation is needed

        // $this->performAjaxValidation($model);


        if (isset($_POST['Comment']))

        {

            if (isset($_POST['Comment']['title']))

                $model->title = $_POST['Comment']['title'];


            if (isset($_POST['Comment']['body']))

                $model->body = $_POST['Comment']['body'];


            $model->save();

        }

    }

}

Lastly, here is the MySQL table:


CREATE  TABLE IF NOT EXISTS `sp_db`.`comment` (

  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,

  `user_id` INT UNSIGNED NOT NULL ,

  `content_id` INT UNSIGNED NOT NULL ,

  `title` VARCHAR(255) NULL ,

  `body` TEXT NULL ,

  `created` DATETIME NOT NULL ,

  `updated` DATETIME NOT NULL ,

  PRIMARY KEY (`id`) ,

  INDEX `fk_comment_content` (`content_id` ASC) ,

  INDEX `fk_comment_user` (`user_id` ASC) ,

  CONSTRAINT `fk_comment_content`

    FOREIGN KEY (`content_id` )

    REFERENCES `sp_db`.`content` (`id` )

    ON DELETE CASCADE

    ON UPDATE CASCADE,

  CONSTRAINT `fk_comment_user`

    FOREIGN KEY (`user_id` )

    REFERENCES `sp_db`.`user` (`id` )

    ON DELETE CASCADE

    ON UPDATE CASCADE)

ENGINE = InnoDB;


parent::beforeSave()

=>


return parent::beforeSave()

PS. I think you’re a perl (or ex-perl) dev :)

Thank you!

I figured it was something simple that I was overlooking.

Interesting guess, but just C, C++, Java, and PHP for me so far.

Well, I guessed perl because in perl the last computed value is automatically returned.

Sorry for offtopic :)

Hi, my experience is that when the Netbeans IDE creates this ‘beforeSave()’ extended method it is created without the ‘return’ word. And I needed minutes and this topic to figure it out what the problem is.

Have a nice day

Laszlo from Hungary

wooooowwww…

Thanks ORey it is also helps me.

Thanks lot.

Time and again I do this same mistake and come back to this same post.

Slap myself in the face and start over again.