Page 1 of 1
Newbie: Problem Storing From A Model
#1
Posted 20 November 2012 - 09:24 AM
Hi,
I used CRUD to generate a model.
I am trying to store a record with it.
Here is what I do:
$model = new NewsModel;
$model->attributes = array(
'id' => '',
'user_id' => Yii::app()->user->id,
'title' => $this->title,
'description' => $this->description,
'active' => 1,
'store_date' => date('Y-m-d H:i:s'),
);
$model->store();
when I run the code, I don't get an error message, but there is no record stored in the database.
Can anyone help me with this one?
In general, some resources about specifics to YII MVC would be highly appreciated, as I use Yii for a couple of days.
I use XAMP/Windows, with MySQL as database
Thanks,
Saylor
I used CRUD to generate a model.
I am trying to store a record with it.
Here is what I do:
$model = new NewsModel;
$model->attributes = array(
'id' => '',
'user_id' => Yii::app()->user->id,
'title' => $this->title,
'description' => $this->description,
'active' => 1,
'store_date' => date('Y-m-d H:i:s'),
);
$model->store();
when I run the code, I don't get an error message, but there is no record stored in the database.
Can anyone help me with this one?
In general, some resources about specifics to YII MVC would be highly appreciated, as I use Yii for a couple of days.
I use XAMP/Windows, with MySQL as database
Thanks,
Saylor
#2
Posted 20 November 2012 - 09:43 AM
The method to store is save(). Try this to determine what the error actually is:
Also note that you can set attributes like so:
if (!$model->save) print_r($model->getErrors());
Also note that you can set attributes like so:
$model->id = ''; $model->user_id = Yii::app()->user->id; . . .
#3
Posted 21 November 2012 - 09:42 AM
Hi,
I did all of the tasks, both by trying to set AR by the attributes array and initializing individual attributes.
The error I receive is: Array ( [id] => Array ( [0] => ID cannot be blank. ) )
As I already mentioned, I have a CRUD generated model, and a PK in the table which cannot be blank.
How can I store?
Thanks,
Saylor
I did all of the tasks, both by trying to set AR by the attributes array and initializing individual attributes.
The error I receive is: Array ( [id] => Array ( [0] => ID cannot be blank. ) )
As I already mentioned, I have a CRUD generated model, and a PK in the table which cannot be blank.
How can I store?
Thanks,
Saylor
Keith, on 20 November 2012 - 09:43 AM, said:
The method to store is save(). Try this to determine what the error actually is:
Also note that you can set attributes like so:
if (!$model->save) print_r($model->getErrors());
Also note that you can set attributes like so:
$model->id = ''; $model->user_id = Yii::app()->user->id; . . .
#4
Posted 21 November 2012 - 09:54 AM
It sounds like your database id field isn't configured as an auto incrementing field. Change the field in the database to ensure that it's both the primary key and auto incrementing.
#5
Posted 21 November 2012 - 10:02 AM
Hi, here the dump of the table structure:
CREATE TABLE IF NOT EXISTS `tbl_news` (
`id` int(11) unsigned NOT NULL,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(5000) NOT NULL DEFAULT '',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tbl_news` (
`id` int(11) unsigned NOT NULL,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(5000) NOT NULL DEFAULT '',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#7
Posted 21 November 2012 - 10:06 AM
You need to mark the id field with the AUTO_INCREMENT attribute. It should look like this:
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
#8
Posted 21 November 2012 - 10:07 AM
Is it the same error or a different one? If you're caching your model schemas then your app will still be seeing stale data. Try turning off schema caching temporarily.
#9
Posted 21 November 2012 - 10:12 AM
Hi, disabled te following row in config/main.php
// 'cache' => array('class' => 'CDummyCache'),
still the same error
// 'cache' => array('class' => 'CDummyCache'),
still the same error
#10
Posted 21 November 2012 - 10:20 AM
Hi,
After regenerating the model, everything is fine now
Huge thanks for the support,
!
Saylor
After regenerating the model, everything is fine now

Huge thanks for the support,

Saylor
#11
Posted 21 November 2012 - 10:20 AM
Make sure 'id' doesn't appear in your 'required' validators in the model rules. It may have been generated by CRUD if no AUTO_INCREMENT attribute was found.
EDIT: I suspect this was the case judging by the post above.
EDIT: I suspect this was the case judging by the post above.
Share this topic:
Page 1 of 1