Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 61
ParticipantSearch
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
42.00
0.00% covered (danger)
0.00%
0 / 61
 attributes
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 7
 rules
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 7
 scenarios
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 search
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 44
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Participant;
/**
 * ParticipantSearch represents the model behind the search form about `common\models\Participant`.
 */
class ParticipantSearch extends Participant
{
    /**
     * @inheritdoc
     */
    public function attributes()
    {
        // add related fields to searchable attributes
        return array_merge(parent::attributes(), [
            'user.firstname',
            'user.lastname',
            'organization.name',
        ]);
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'is_support_only', 'created_at', 'updated_at', 'organization_id', 'user_id'], 'integer'],
            [['phone', 'description', 'image'], 'safe'],
            [['user.firstname', 'user.lastname', 'organization.name'], 'safe'],
        ];
    }
    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }
    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $query = null)
    {
        if(!$query){
            $query = Participant::find();
        }
        $query
            ->joinWith(['user' => function($q){ $q->from(['user' => '{{%user}}']); }], false)
            ->joinWith(['organization' => function($q){ $q->from(['organization' => '{{%organization}}']); }], false);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->sort->attributes['user.firstname'] = [
            'asc' => ['user.firstname' => SORT_ASC],
            'desc' => ['user.firstname' => SORT_DESC],
        ];
        $dataProvider->sort->attributes['user.lastname'] = [
            'asc' => ['user.lastname' => SORT_ASC],
            'desc' => ['user.lastname' => SORT_DESC],
        ];
        $dataProvider->sort->attributes['organization.name'] = [
            'asc' => ['organization.name' => SORT_ASC],
            'desc' => ['organization.name' => SORT_DESC],
        ];
        $dataProvider->sort->defaultOrder = [
            'user.lastname' => SORT_ASC,
            'user.firstname' => SORT_ASC,
        ];
        $this->load($params);
        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        $query->andFilterWhere([
            'id' => $this->id,
            'is_support_only' => $this->is_support_only,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'organization_id' => $this->organization_id,
            'user_id' => $this->user_id,
        ]);
        $query->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'image', $this->image]);
        
        $query->andFilterWhere(['like', 'user.firstname', $this->getAttribute('user.firstname')])
            ->andFilterWhere(['like', 'user.lastname', $this->getAttribute('user.lastname')])
            ->andFilterWhere(['like', 'organization.name', $this->getAttribute('organization.name')]);
        return $dataProvider;
    }
}