This is a common situation for programmers. For example, these check boxes aim to delete these selected records. First, let's define a very useful 'polymorphic 'form. I believe you will use it!
This is a common situation for programmers. For example, these check boxes aim to delete these selected records.
First, let's define a very useful 'polymorphic 'form. I believe you will use it!
data[$key]) ? $this->data[$key] : null); } public function __set($key, $value) { $this->data[$key] = $value; } } ?>
To continue the model, let's define these fields in the 'example 'table: keyExample, Field_one, Field_two. You know how to do it, right ?...
Then, in the controller ExampleAction. php, process the 'example 'list in the action.
... // an over simplified 'list' action public function actionList () { $form = new PolymorphicForm; $this->render("list", array( "models" => Example::model()->findAll(), "form" => $form, )); } ...
Then define the view in the file list. php.
"myForm")); ?>
|
|
All "checkAll")); ?> Delete |
$rec): ?>
Field_one); ?> |
Field_two); ?> |
keyExample"); ?> |
Finally, we cleverly handle these checkboxs in ExampleAction. php:
... /*** Deletes a range of model* If deletion is successful, the browser will be redirected to the "list" page. */ public function actionDelete() { // I want a post if(Yii::app()->request->isPostRequest) { // parse $_POST variables foreach($_POST["PolymorphicForm"] as $key => $val) { // is one a these checkbox ? if (strstr ($key, "checkRecord")) { // checkbox in state checked ? if ($val == 1) { // get the key of the record $ar = explode ("_", $key); // deleting record $model = Example::model()->findByPk ($ar[1])->delete (); } } } $this->redirect(array("list")); } else throw new CHttpException(400,"Invalid request. Please do not repeat this request again."); } ...
The result is as follows: http: // myHttpServer/myApp? R = Example/list can be verified
I hope this will be especially helpful to some Yii programmers.