Just Before Finish


CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog.tbl_post, CONSTRAINT FK_post_author FOREIGN KEY (author_id) REFERENCES tbl_user (id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_post (title, content, tags, status, update_time, create_time, author_id) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)


342 return $n;

343 }

344 catch(Exception $e)

345 {

346 if($this->_connection->enableProfiling)

347 Yii::endProfile(‘system.db.CDbCommand.execute(’.$this->getText().’)’,‘system.db.CDbCommand.execute’);

348 $errorInfo = $e instanceof PDOException ? $e->errorInfo : null;

349 $message = $e->getMessage();

350 Yii::log(Yii::t(‘yii’,‘CDbCommand::execute() failed: {error}. The SQL statement executed was: {sql}.’,

351 array(’{error}’=>$message, ‘{sql}’=>$this->getText().$par)),CLogger::LEVEL_ERROR,‘system.db.CDbCommand’);

352 if(YII_DEBUG)

353 $message .= '. The SQL statement executed was: '.$this->getText().$par;

354 throw new CDbException(Yii::t(‘yii’,‘CDbCommand failed to execute the SQL statement: {error}’,

355 array(’{error}’=>$message)),(int)$e->getCode(),$errorInfo);

356 }

357 }


359 /**

360 * Executes the SQL statement and returns query result.

361 * This method is for executing an SQL query that returns result set.

362 * @param array $params input parameters (name=>value) for the SQL execution. This is an alternative

363 * to {@link bindParam} and {@link bindValue}. If you have multiple input parameters, passing

364 * them in this way can improve the performance. Note that if you pass parameters in this way,

365 * you cannot bind parameters or values using {@link bindParam} or {@link bindValue}, and vice versa.

366 * binding methods and the input parameters this way can improve the performance.

Stack Trace


  • C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(1014): CDbCommand->execute()


  • C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(787): CActiveRecord->insert(null)


– C:\xampp\htdocs\blog2\protected\controllers\PostController.php(67): CActiveRecord->save()

62 {

63 $model=new Post;

64 if(isset($_POST[‘Post’]))

65 {

66 $model->attributes=$_POST[‘Post’];

67 if($model->save())

68 $this->redirect(array(‘view’,‘id’=>$model->id));

69 }


71 $this->render(‘create’,array(

72 ‘model’=>$model,


  • C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(50): PostController->actionCreate()


  • C:\xampp\htdocs\yii\framework\web\CController.php(309): CInlineAction->runWithParams(array())


  • C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(134): CController->runAction(CInlineAction)


  • C:\xampp\htdocs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()


  • C:\xampp\htdocs\yii\framework\web\CController.php(1146): CFilter->filter(CFilterChain)


  • C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)


  • C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)


  • C:\xampp\htdocs\yii\framework\web\CController.php(292): CFilterChain->run()


  • C:\xampp\htdocs\yii\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl"))


  • C:\xampp\htdocs\yii\framework\web\CWebApplication.php(276): CController->run("create")


  • C:\xampp\htdocs\yii\framework\web\CWebApplication.php(135): CWebApplication->runController("post/create")


  • C:\xampp\htdocs\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()


– C:\xampp\htdocs\blog2\index.php(13): CApplication->run()

08 defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

09 // specify how many levels of call stack should be shown in each log message

10 defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,5);


12 require_once($yii);

13 Yii::createWebApplication($config)->run();

2012-10-08 17:42:52 Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_p][/code]

That suggests that you’re specifying an author_id that doesn’t exist in the tbl_user table. Make sure that the author_id is being set correctly and that it corresponds to a valid user.

i guess you mean set it up in my sql (i use myphpadmin)

so how should it be setup right?


No, you just need to make sure that there’s a row in the user table corresponding to the author_id. Make sure that the author_id you’re trying to save exists in the user table.


i do understand what u say

but i dont know how to do that

how can i check if its exist at all?

sorry i know it sound silly but i realy new in this :)

Usually, that would be handled through your form/view. a pick list for authors (select * from authors table), or something like that to ensure that the author exists.

How does a user pick an author? If they just type it in (author_id), might want to check that against the author table prior to insert, update and delete.

so this is my post model

and it check the author

where else it should be checked?

public function rules()


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

	// will receive user inputs.

	return array(

		array('title, content, status, author_id', 'required'),

		array('status', 'in', 'range'=>array(1,2,3)),

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

		array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),

		array('tags', 'normalizeTags'),

		array('title, status', 'safe', 'on'=>'search'),



From that code, it doesn’t seem that you’re verifying that the author exists at all.

I suggest you go through the full Yii guide to get a grip on Active Record before continuing further.


tnx all, guys.