dongruyan4948 2013-09-16 19:28
浏览 47

如何在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 - 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:

  1. Category has many Subcategory
  2. 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:

  1. Display Categories as a listview
  2. Once the category clicked, then display SubCategories as a listview
  3. Once the subcategory clicked, then display Ads as listview
  4. 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.

  • 写回答

1条回答 默认 最新

  • douyouyi8878 2013-09-16 19:38
    关注

    You can iterate subcategories as follows (name variables as in your generated code). $category is the loaded model for which you will display subcategories for.

    foreach($category->subcategories as $subcategory)
    {
        // you have your subcategory 
    }
    

    Similarly, you can iterate ads the same way:

    foreach($subcategory->ads as $ad)
    {
         // you have all ads under this subcategory
    }
    

    You can put the second code inside first to have all of them.

    This can be in view or controller, it depends on how you want to display...

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本