Questions Related To The Deletebypk() ?

/**

 * 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()-&gt;user-&gt;setFlash('success','error'); // Always perform here...<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />


		&#036;this-&gt;redirect('./index.php?r=ytadmin/Content/index&amp;type='.&#036;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…

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.

Kindy check your database first if the pk that you passed on the parameter exists.

public function actionDel($type, $id) {

	var_dump(&#036;type::model()-&gt;deleteByPk(&#036;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…

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

Try to use if($type::model()->findByPk($id)->delete()) instead.