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 - http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/#hh13
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:
- Display Categories as a listview
- Once the category clicked, then display SubCategories as a listview
- Once the subcategory clicked, then display Ads as listview
- Once the ads clicked, then display the Ad as detail
Looking the point 2-4 above, how do we modify this ApiController that we have to do the following below?
- List SubCategory by CategoryID
- List Ad by SubCategoryID
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'm appreciated your help.