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;