How To Add New Action In Yii Framework?

We have this PHP application already NOT using any framework. We are taping into Yii framework for building webservices for our simple mobile app. We are quite new to Yii framework.

We follow this article and managed to get to work - REST-API-Article

Mainly it just created ApiController.php and extended the Controller class.

We have 3 tables in databases: Category, Subcategory, Ad. The table relation as follow:


Category has many Subcategory

Subcategory has many Ad

At the moment by default after doing CRUD process, it generates the codes for Category, Subcategory and Ad. The issue that we have that this codes generates for actionList as select entire records.

Here’s the view for our simple mobile app as follow:

Looking the point 2-4 above, how do we modify this ApiController that we have to do the following below?

Here’s the actionList of ApiController.php that we’ve created based on the article above:


// {{{ actionList

    public function actionList()

    {

        // $this->_checkAuth();


        switch($_GET['model'])

        {

            case 'category': // {{{ 

                $models = Category::model()->findAll();

                break; // }}} 

            case 'ad': // {{{ 

                $models = Ad::model()->findAll();

                break; // }}} 

            case 'subcategory': // {{{ 

                $models = Subcategory::model()->findAll();

                break; // }}}       

            default: // {{{ 

                $this->_sendResponse(501, sprintf('Error: Mode <b>list</b> is not implemented for model <b>%s</b>',$_GET['model']) );

                exit; // }}} 

        }

        if(is_null($models)) {

            $this->_sendResponse(200, sprintf('No items where found for model <b>%s</b>', $_GET['model']) );

        } else {

            $rows = array();

            foreach($models as $model)

                $rows[] = $model->attributes;


            $this->_sendResponse(200, CJSON::encode($rows));

        }

    } // }}} 



I am appreciated your feedback.

in the controller class you just have to write a new actionXY in the class and then add the action to the accessRules() method:




// Add the actionAjaxIndex & actionAjaxSave actions here.

public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view', 'ajaxIndex'), // added the ajaxIndex action here

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete', 'ajaxSave'), //added the ajaxSave action here.

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


// {{{ actionList

    public function actionList()

    {

        // $this->_checkAuth();


        switch($_GET['model'])

        {

            case 'category': // {{{ 

                $models = Category::model()->findAll();

                break; // }}} 

            case 'ad': // {{{ 

                $models = Ad::model()->findAll();

                break; // }}} 

            case 'subcategory': // {{{ 

                $models = Subcategory::model()->findAll();

                break; // }}}       

            default: // {{{ 

                $this->_sendResponse(501, sprintf('Error: Mode <b>list</b> is not implemented for model <b>%s</b>',$_GET['model']) );

                exit; // }}} 

        }

        if(is_null($models)) {

            $this->_sendResponse(200, sprintf('No items where found for model <b>%s</b>', $_GET['model']) );

        } else {

            $rows = array();

            foreach($models as $model)

                $rows[] = $model->attributes;


            $this->_sendResponse(200, CJSON::encode($rows));

        }

    } // }}} 


       // ...Codes ...

       // ............. Just create actions ........


       /**

        * List all models by Ajax request.

        */

       public function actionAjaxIndex() {


           Yii::app()->language='hu';


           //... codes ...

       }

        

       /**

        * Save model by Ajax call.

        * @retrun string error message.

        */

       public function actionAjaxSave() {


               // do something


       }



When we adjust the Controller, do we need to adjust the Model as well?

To achieve what we want as follow:


List of SubCategory by CategoryID

List of Ad by SubCategoryID

How do we pass this CategoryID in this SubCategory as well as SubCategoryID in Ads?