Advertisement
Guest User

Yii2 REST api Search

a guest
May 11th, 2014
1,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @link http://www.yiiframework.com/
  5.  * @copyright Copyright (c) 2008 Yii Software LLC
  6.  * @license http://www.yiiframework.com/license/
  7.  */
  8.  
  9. namespace app\core\rest;
  10.  
  11. use Yii;
  12. use yii\data\ActiveDataProvider;
  13. use yii\rest\Action;
  14.  
  15. /**
  16.  * @author Rajan Rauniyar <rajanrauniyar@gmail.com>
  17.  * @since 2.0
  18.  */
  19. class SearchAction extends Action {
  20.  
  21.     /**
  22.      * @var callable a PHP callable that will be called to prepare a data provider that
  23.      * should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
  24.      * The signature of the callable should be:
  25.      *
  26.      * ```php
  27.      * function ($action) {
  28.      *     // $action is the action object currently running
  29.      * }
  30.      * ```
  31.      *
  32.      * The callable should return an instance of [[ActiveDataProvider]].
  33.      */
  34.     public $prepareDataProvider;
  35.     public $params;
  36.  
  37.     /**
  38.      * @return ActiveDataProvider
  39.      */
  40.     public function run() {
  41.         if ($this->checkAccess) {
  42.             call_user_func($this->checkAccess, $this->id);
  43.         }
  44.  
  45.         return $this->prepareDataProvider();
  46.     }
  47.  
  48.     /**
  49.      * Prepares the data provider that should return the requested collection of the models.
  50.      * @return ActiveDataProvider
  51.      */
  52.     protected function prepareDataProvider() {
  53.         if ($this->prepareDataProvider !== null) {
  54.             return call_user_func($this->prepareDataProvider, $this);
  55.         }
  56.  
  57.         /**
  58.          * @var \yii\db\BaseActiveRecord $modelClass
  59.          */
  60.         $modelClass = $this->modelClass;
  61.        
  62.         $model = new $this->modelClass([
  63.         ]);
  64.  
  65.         $safeAttributes = $model->safeAttributes();
  66.         $params = array();
  67.        
  68.         foreach($this->params as $key => $value){
  69.             if(in_array($key, $safeAttributes)){
  70.                $params[$key] = $value;                
  71.             }
  72.         }
  73.  
  74.         $query = $modelClass::find();
  75.  
  76.         $dataProvider = new ActiveDataProvider([
  77.             'query' => $query,
  78.         ]);
  79.  
  80.         if (empty($params)) {
  81.             return $dataProvider;
  82.         }
  83.  
  84.  
  85.         foreach ($params as $param => $value) {
  86.             $query->andFilterWhere([
  87.                 $param => $value,
  88.             ]);
  89.         }
  90.        
  91.         return $dataProvider;
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement