Binding Urls With Action

Hi all,

I am trying to create CRUD operations to a table with composite keys,

In my controller i have overridden the conventional set of functions to accept two parameters. The create action gets executed but my view, update and deletion functions from CGridview doesnt work.

in my Controller




public function actionView($subjectName, $teacher_id)

{

   $model=$this->loadModel($subjectName, $teacher_id);

   $this->render('view',array('model'=>$model));

}


public function loadModel($subjectName, $teacher_id)

{

$model=  Teacherteachsub::model()->findByPk(array('subjectName'=>$subjectName, 'subjectTeacherId'=>$teacher_id));


if($model==null)

   throw new CHttpException(404,'The requested page does not exist.');

return $model;

}



In my admin.php i bind the current selected values to the URL as:-




<?php

$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'resultgrid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'subjectName',

        'subjectTeacherId',

        array(

            'class' => 'CButtonColumn',

            'template' => '{view}{update}{delete}',

            'buttons' => array

                (

                'view' => array

                    (

                    'url' =>

                    'Yii::app()->createUrl("teacherteachsub/view/",array("subjectName"=>$data->subjectName, "subjectTeacherId"=>$data->subjectTeacherId))',

                ),

            ),

        ),

    ),

));


?>



What i get is :

I used the debugger and it gives a similar error. As far as i can understand the way i have called the action from URL is correct, have i missed something?

Any help to this right is greatly appreciated, Thanks for your time!

Cheerz! :)

Could you please post the code of your actionUpdate() and actionDelete() functions? Those functions may use loadModel() function, which you had modified before.

@rei - Thanks for the reply,

actionUpdate()




public function actionUpdate($subjectName, $teacher_id) 

{

$model = $this->loadModel($subjectName, $teacher_id);

if (isset($_POST['Teacherteachsub'])) 

{

            $model->attributes = $_POST['Teacherteachsub'];

            $this->saveModel($model);

            $this->redirect(array('view', 'subjectName' => $model->subjectName, 'subjectTeacherId' => $model->subjectTeacherId));

}

$this->render('update', array('model' => $model));

}



actionDelete()




public function actionDelete($subjectName, $teacher_id) 

{

        if (Yii::app()->request->isPostRequest) 

            {

            try 

            {

                $this->loadModel($subjectName, $teacher_id)->delete();

            }

            catch (Exception $e) 

            {

               $this->showError($e);

            }

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

            if (!isset($_GET['ajax']))

                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));

        }

        else

            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');

}



Any help is appreciated…

Thanks

Cheerz! :)

You need to modify the URLs of update and delete buttons in CGridView because actionUpdate() and actionDelete() functions have 2 parameters now.




array(

   'class' => 'CButtonColumn',

    'template' => '{view}{update}{delete}',

    'viewButtonUrl' => ...,

    'updateButtonUrl' => ...,

    'deleteButtonUrl' => ...,

),



@rei - thanks for the reply, well i had the code for update and delete, i just added the view code only assuming that if that function works i can figure out the code for other two as well, anyways my controller code :-




<?php

$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'resultgrid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'subjectName',

        'subjectTeacherId',

        array(

            'class' => 'CButtonColumn',

            'template' => '{view}{update}{delete}',

            'buttons' => array

                (

                'view' => array

                    (

                    'url' =>

                    'Yii::app()->createUrl("teacherteachsub/view/",array("subjectName"=>$data->subjectName, "subjectTeacherId"=>$data->subjectTeacherId))',

                ),

                

                'update' => array

                    (

                    'url' =>'Yii::app()->createUrl("teacherteachsub/update/",array("subjectName"=>$data->subjectName, "subjectTeacherId"=>$data->subjectTeacherId))',

                ),

                'delete' => array

                    (

                    'url' =>'Yii::app()->createUrl("Teacherteachsub/delete/",array("subjectName"=>$data->subjectName, "subjectTeacherId"=>$data->subjectTeacherId))',

                ),

            ),

        ),

    ),

));


?>



I assumed that the parameters were not being passed, cz i get a 400 error as mentioned above…

Can you figure out anything wrong with this… it seems correct… :)

Any help is appreciated!

Cheerz!

:)!

You can check if the parameters were passed or not in URL/address bar, can’t you?

By the way, in your view you use ‘subjectTeacherId’ as your parameter, whereas in controller the parameter is ‘teacher_id’. Have you tried to use the same name?

@rei - Thanks a lot for the reply, I changed the parameter name and it worked!! I thought they only consider the value being passed, like in java…

Anyways, thanks a lot…! :))!

Cheerz!