chenyan
(578672331)
February 25, 2014, 3:22am
1
/**
* delete article
* @param int $id
*/
public function actionDel($type, $id) {
if($type::model()->deleteByPk($id)) { // do delete
Yii::app()->user->setFlash('success','ok');
$this->redirect('./index.php?r=ytadmin/Content/index&='.$type);
} else {
Yii::app()->user->setFlash('success','error'); // Always perform here...<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />
$this->redirect('./index.php?r=ytadmin/Content/index&type='.$type);
}
}
As you see,It is used to delete the articles.
It can perform successfully
but return value is ‘0’,this why
I really don’t understand…
topeyaki
(Christopherp)
February 25, 2014, 4:23am
2
If the return value when performing deleteByPk is 0, then the method was not able to find the ID of the record to delete or 0 records deleted.
mateeyow
(Matthewt)
February 25, 2014, 4:25am
3
Kindy check your database first if the pk that you passed on the parameter exists.
chenyan
(578672331)
February 25, 2014, 5:12am
4
public function actionDel($type, $id) {
var_dump($type::model()->deleteByPk($id));die;
}
You see,Print the results are as follows:
int(0)
But…the data has been deleted of database
so,I really don’t understand…
why…
topeyaki
(Christopherp)
February 25, 2014, 5:24am
5
The return value is the number of row entry affected by the delete. So if you deleted one entry then it returns 1, 0 for none, and n for n number of row entry deleted.
Try deleting another row entry.
-correction: deleteByPk returns either 1 or 0.
deleteAll returns n (number of deleted row) or 0 for none
qianggan
(qianggan@msn.com)
February 27, 2014, 8:02pm
6
Try to use if($type::model()->findByPk($id)->delete()) instead.