Blame view

frontend/models/SearchModel.php 1.44 KB
9afd152f   Anastasia   - support from main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <?php
      /**
       * Created by PhpStorm.
       * User: stes
       * Date: 22.06.18
       * Time: 12:40
       */
      
      namespace frontend\models;
      
      use yii\base\Model;
      use yii\db\Expression;
      use yii\db\Query;
      use yii\web\JsExpression;
  
      class SearchModel extends Model
      {
          public $text;
          
          public $author;
          
          public $bookTitle;
          
          public function rules()
          {
              return [
                  [
                      'text',
                      'required',
                      'whenClient' => new JsExpression('function (attribute, value) {
                          return $("#next").data("id") == "";
                      }'),
                  ],
                  [
                      ['author', 'bookTitle'],
                      'boolean'
                  ],
                  [
                      'text', 'string'
                  ]
              ];
          }
          
          public function search(){
              $query = (new Query())->select('book.id')->from('book')->join('INNER JOIN', 'author', 'book.author_id = author.id');
              if ($this->author){
                  $query->where(['ilike', 'book.title', $this->text]);
              }
              if ($this->bookTitle){
                  $query->orWhere(['ilike', 'secondname', $this->text])->orWhere(['ilike', new Expression('concat(name,\' \',secondname)'), $this->text]);
              }
              return $query->column();
          }
      }