Pergunta

I am using CGRidVeiw to list my records from database, In each records I have an edit and delete button.Everthing is working fn9 other than the delete operation,actually its is showing an error after clicking the delete icon,but its still working.Below is the snap of what it actually look like,

enter image description here

After hitting refresh its not showing the record. I have my view and contoller as below.

//contoller

  public function actionDeleteEmployer() {

        $model = new AdminDeleteEmployer();

        if (isset($_GET['id'])) {
            $id = $_GET['id'];
        }

        $query = "DELETE FROM user,jobs USING user INNER JOIN jobs ON jobs.user_id = user.id WHERE user.id =$id";

        Yii::app()->db->createCommand($query)->queryAll();

         $this->redirect('admin/site/ManageEmployers');
    }

///view

Manage Employers

<div class="form">
    <?php
    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $model->search(),
        'ajaxUpdate'=>false,
        //   'filter' => $model,
        'columns' => array(
            array(
                'name' => ' Employer ID',
                'type' => 'raw',
                'value' => 'CHtml::encode($data->id)',
                'htmlOptions' => array('style' => 'width:90px;', 'class' => 'zzz'),
            // 'filter'=>'false' /* for hiding filter boxes */
            ),
            array(
                'name' => 'Created On',
                'type' => 'raw',
                'value' => 'CHtml::encode( date("m-d-Y",strtotime($data->created)))',
                'htmlOptions' => array('style' => 'width:90px;', 'class' => 'zzz'),
            // 'filter'=>'false' /* for hiding filter boxes */
            ),
            array(
                'name' => 'Account Name',
                'type' => 'raw',
                'value' => 'CHtml::encode($data->name)',
                'htmlOptions' => array('style' => 'width:90px;', 'class' => 'zzz'),
            // 'filter'=>'false' /* for hiding filter boxes */
            ),
            array(
                'name' => 'Email',
                'type' => 'raw',
                'value' => 'CHtml::encode($data->email)',
                'htmlOptions' => array('style' => 'width:90px;', 'class' => 'zzz'),
            // 'filter'=>'false' /* for hiding filter boxes */
            ),
            array(
                'name' => 'Last Active On',
                'type' => 'raw',
                'value' => 'CHtml::encode( date("m-d-Y",strtotime($data->modified)))',
                'htmlOptions' => array('style' => 'width:90px;', 'class' => 'zzz'),
            // 'filter'=>'false' /* for hiding filter boxes */
            ),
            array(
                'class' => 'CButtonColumn',
                'deleteConfirmation' => 'Are you sure you want to delte this item?',
                'template' => '{view}{delete}',
                'buttons' => array(
                    'view' => array('label' => 'view',
                        'url' => 'Yii::app()->controller->createUrl("ViewEmployerActivity",array("id"=>$data["id"]))'),
                    'delete' => array('label' => 'delete',
                        'url' => 'Yii::app()->controller->createUrl("DeleteEmployer",array("id"=>$data["id"]))'),
                )
            )
        ),
    ));
    ?>

</tbody>
</table>
Foi útil?

Solução

redirect to admin action

Yii::app()->db->createCommand($query)->execute();
$this->redirect(array('admin'))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top