In Chapter 4 "Project CRUD" section "Create a new project" page 68 of 304 the author reports a successful save of a new project when only the required fields "name" and "description" are filled in.
However, I met with the following error, I am using version 1.1.13 of Yii Framework:
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: ‘’ for column ‘create_time’ at row 1. The SQL statement executed was: INSERT INTO tbl_project
(name
, description
, create_time
, create_user_id
, update_time
, update_user_id
) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5)
M:\Martin\yiiframework\framework\db\CDbCommand.php(357)
As can be seen from the error message, the invalid datetime value is ‘’ i.e. the empty string. It is the value of parameter yp2, which is the value for the column create_time.
Because the book reports a successful save with version 1.1.12 and I find this error with version 1.1.13 I conclude that something in the code has changed and the change has led to this bug.
I have debugged the code execution and found that things go wrong in file CDbColumnSchema.php in the public function typecast($value)
I believe that I have found a bugfix, but as I am quite a beginner with Yii, I would appreciate comments on this bugfix, and maybe there are better ways to fix this, or maybe my fix has some undesired side-effects.
Here follows my changed code for CDBColumnSchema.php public function typecast($value) :
public function typecast($value)
{
// MartindeGroot 2013-07-07
// problem description: a MySQL datetime column has property dbType=='datetime' but it has property type=='string'
// and with the unchanged code here a $value== '' (empty string) is typecast to '' but it should be typecast to null
// a $value=='' in the unchanged code meets the first condition (gettype($value)===$this->type, both are type 'string' and so this $value '' is immediately returned
// this leads to the error "invalid datetime value '' for a datetime column. the value to be returned in this case should be null, not ''
// MdG begin of OLD CODE
// if(gettype($value)===$this->type || $value===null || $value instanceof CDbExpression)
// return $value;
// if($value===’’ && $this->allowNull)
// return $this->type===‘string’ ? ‘’ : null;
// MdG end of OLD CODE
// MdG begin of NEW CODE
if( (gettype($value)===$this->type && $value !='') || $value===null || $value instanceof CDbExpression)
return $value;
if($value==='' && $this->allowNull)
return ($this->type==='string' && $this->dbType != 'datetime') ? '' : null;
// MdG end of my changed code
switch($this->type)
{ .... rest of function unchanged
If it is more appropriate to put this post in another secion of the forum, or maybe it should be a bug report, I kindly ask a forum administrator to place this post in the proper section.
Hope this is useful to other readers of this book who may have come across this same error when following along with this Chapter 4 section Create a new Project.
Martin de Groot