Subaction in action?

I have a section on my website where people can edit their Company information (index.php?r=company/update&id=42). People can also add, edit or delete extra information (as much as they want, it’s a single line of text). This extra information is stored in a table called company_extra (with a model called CompanyExtra). Now i want the CRUD actions to be available for this extra information (which is related to the company).

So i need some sort of subactions. I have done this in my Company Controller like this, so i can call index.php?r=company/extra&id=42&sa=update&eId=43:




<?php

public function actionExtra($id, $sa=null, $eId=null)

	{

        $model = $this->loadModel($id);


        if ($model->userId !== Yii::app()->user->getId()) {

            throw new CHttpException(403, 'Je hebt geen toegang tot deze actie');

        }


        switch($sa) {


            // Create new extra info

            case 'create':


              $eModel=new CompanyExtra;


              // Uncomment the following line if AJAX validation is needed

              //$this->performAjaxValidation($eModel);


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

              {

                  $eModel->attributes=$_POST['CompanyExtra'];

                  $eModel->companyId=$model->id;


                  if($eModel->save())

                      $this->redirect(array('view','id'=>$eModel->id));

              }


              $this->render('extra',array(

                  'model'=>$model,

                  'eModel'=>$eModel,

                  'subaction'=>'create'

              ));


            break;


            // Update extra info

            case 'update':

            // Update part here

            break;


            // Manage extra info

            default:


              $dataProvider=new CActiveDataProvider('CompanyExtra', array(

                  'criteria'=>array(

                      'condition'=>'companyId='.$model->id

                  ),

                  'pagination'=>array(

                      'pageSize'=>15,

                  ),

              ));


              $this->render('extra', array(

                  'dataProvider' => $dataProvider,

                  'model' => $model,

                  'subaction'=>'manage'

              ));


            break;


        }

}

?>



Is this a good way to do this? Or are there better alternatives?

I would create separate crud actions for companyExtra in your companyController. addExtraAction, removeExtraAction, updateExtraAction sound ok for me :). IMO actions should result in setting flash message and redirecting back to profile view, instead of rendering views by themselves. That will limit users confusion as they will see the same address all the time.

Yeah, that sounds as a good idea indeed!