chenyan
(578672331)
1
/**
* 删除文章
* @param int $id
*/
public function actionDel($type, $id) {
if($type::model()->deleteByPk($id)) { // 执行和判断删除语句
Yii::app()->user->setFlash('success','删除文章成功');
$this->redirect('./index.php?r=ytadmin/Content/index&='.$type);
} else {
Yii::app()->user->setFlash('success','删除文章失败'); // 总是走这个
$this->redirect('./index.php?r=ytadmin/Content/index&type='.$type);
}
}
上面的我写的删除文章的函数,操作是可以成功的,去数据库看了下,也是删除了的,但是让人不明的的就是删除后的返回值是0,为什么呢?我明明删除成功了呀?求解。。。。
qianggan
(qianggan@msn.com)
2
public function deleteByPk($pk,$condition='',$params=array())
{
Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createPkCriteria($this->getTableSchema(),$pk,$condition,$params);
$command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
return $command->execute();
}
$command->execute(); Executes the SQL statement. This method is meant only for executing non-query SQL statement. No result set will be returned.
Is your database is MySQL?
qianggan
(qianggan@msn.com)
3
Maybe you can try $type::model()->findByPk($id)->delete(); Which will return Boolean value.
See source code at:
It shows $n=$this->_statement->rowCount(); Not all database will return this value correctly.