Load Model Record, Change Value, Saving Gives Errors

When I load the model record below, change one value and try to save it, I get errors for every field that is a bit(1) (the bFieldName fields in the model code below) stating "Field must be an integer."


$model = Student::model()->findByPk(123);

$model->FieldName = 'abc';

$model->save();

Model:


public function rules() {

   return array(

      array('strFirstName, strLastName, nUserID, strEmail, strPhone, dateCreated', 'required'),

      array('nUserID, bPhoto, bRecordings, bActive, nBillingDays, nLockDays', 'numerical', 'integerOnly'=>true),

      array('strFirstName, strLastName', 'length', 'max'=>16),

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

      array('strPhone', 'length', 'max'=>10),

      array('strDescription', 'length', 'max'=>1000),

      array('strStudioName', 'length', 'max'=>64),


      array('nTeacherID, strFirstName, strLastName, nUserID, strEmail, strPhone, bPhoto, bRecordings, bActive, dateCreated, strDescription, strStudioName, nBillingDays, nLockDays', 'safe', 'on'=>'search'),

   );

}



If I look at what value these fields hold, they are empty.

Why doesn’t the model have the values stored in the database for these fields, like the other fields?

Thanks

what is the error?

try this


$model->saveAttributes(array('FieldName'=>'abc'));

Hi webmonk

I can’t see the “FieldName” in your rules.

Are you sure the "FieldName" exist in your AR and in the table of database?

i think thats a common name he used!!

OK, check with echo the $model->FieldName after of

$model = Student::model()->findByPk(123);

echo $model->FieldName; //for testing

Also check the type of fieldName in your database table

Sorry for the confusion about the field name, yes, it was just generic.

Here is what echo’s back from all the fields, bPhoto, bRecordings and bActive all come back blank. I would expect a 1 or 0.

strFirstName: John

strLastName: Teacher

nUserID: 2

strEmail: john@mail.com

strPhone: 9185551212

dateCreated: 2012-11-28 01:18:32

bPhoto:

bRecordings:

bActive:


CREATE TABLE `tblTeachers` (

  `nTeacherID` smallint(6) NOT NULL AUTO_INCREMENT,

  `strFirstName` varchar(16) NOT NULL,

  `strLastName` varchar(16) NOT NULL,

  `nUserID` int(11) NOT NULL,

  `strEmail` varchar(255) NOT NULL,

  `strPhone` varchar(10) NOT NULL,

  `bPhoto` bit(1) NOT NULL DEFAULT b'0',

  `bRecordings` bit(1) NOT NULL DEFAULT b'0',

  `bActive` bit(1) NOT NULL DEFAULT b'1',

  `dateCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (`nTeacherID`),

  KEY `IX_tblTeachers_bActive` (`bActive`),

  KEY `IX_tblTeachers_nUserID` (`nUserID`),

  CONSTRAINT `FK_tblTeachers_nUserID` FOREIGN KEY (`nUserID`) REFERENCES `tblUsers` (`nUserId`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1$$

And here are the matching model rules:


public function rules() {

   // NOTE: you should only define rules for those attributes that

   // will receive user inputs.

   return array(

      array('strFirstName, strLastName, nUserID, strEmail, strPhone, dateCreated', 'required'),

      array('nUserID, bPhoto, bRecordings, bActive', 'numerical', 'integerOnly'=>true),

      array('strFirstName, strLastName', 'length', 'max'=>16),

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

      array('strPhone', 'length', 'max'=>10),


      array('nTeacherID, strFirstName, strLastName, nUserID, strEmail, strPhone, bPhoto, bRecordings, bActive, dateCreated', 'safe', 'on'=>'search'),

   );

}