title: How to get and search an attribute, from a related table, via another related table - one to one relation
body: <p>I have three tables as follows: </p>
  
  <p>policy:(id, r_available_taemin[fk]) --</p>
  
  <p>available_taemin:(id, , name, r_company[fk]) --</p>
  
  <p>company:(id, name)</p>
  
  <p>In the grid view of policies( index) I need to show the company name, and I have done that.
  But now I need to add filtering for the company name, and here I am facing the problems.</p>
  
  <p>My search model is ad follows:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  use yii\base\Model;
  use yii\data\ActiveDataProvider;
  use app\models\appmodels\AppPoliciesGeneral;
  
  /**
   * PoliciesGeneralSearch represents the model behind the search form of `app\models\appmodels\AppPoliciesGeneral`.
   */
  class PoliciesGeneralSearch extends AppPoliciesGeneral {
  
      public $customerName;
      public $companyName;
  
      /**
       * @inheritdoc
       */
      public function rules() {
          return [
              [['policy_id', 'r_deal_type', 'r_customer_id', 'r_available_taemin', 'is_active', 'r_invoice', 'isRenewed'], 'integer'],
              [['policy_type', 'policy_code', 'madmoun_name', 'payment_option', 'contract_date', 'start_date', 'end_date', 'attachment', 'sale_letters', 'paid_customer_letters', 'paid_company_letters', 'table_name', 'deactivated_at', 'created_at', 'updated_at'], 'safe'],
              [['cost', 'price', 'sale', 'tax', 'paid_customer', 'remaining_customer', 'paid_company', 'remaining_company'], 'number'],
              [['customerName', 'companyName'], '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 = AppPoliciesGeneral::find();
  
          // add conditions that should always apply here
          $dataProvider = new ActiveDataProvider([
              'query' =&gt; $query,
          ]);
          $dataProvider-&gt;setSort([
              'attributes' =&gt; [
  
          $query-&gt;joinWith(['rCustomer']);
          $query-&gt;joinWith(['rAvailableTaemin']);
  //        $query-&gt;joinWith(['rCompany']);
  
                  $this-&gt;load($params);
  
                  if(!$this-&gt;validate()) {
                  // uncomment the following line if you do not want to return any records when validation fails
                  // $query-&gt;where('0=1');
                  return $dataProvider;
                  }
  
                  // grid filtering conditions
                  $query-&gt;andFilterWhere([
          'policy_id' =&gt; $this-&gt;policy_id,
                  'r_deal_type' =&gt; $this-&gt;r_deal_type,
                  'r_customer_id' =&gt; $this-&gt;r_customer_id,
                  'r_available_taemin' =&gt; $this-&gt;r_available_taemin,
                  'contract_date' =&gt; $this-&gt;contract_date,
                  'start_date' =&gt; $this-&gt;start_date,
                  'end_date' =&gt; $this-&gt;end_date,
                  'is_active' =&gt; $this-&gt;is_active,
                  'cost' =&gt; $this-&gt;cost,
                  'price' =&gt; $this-&gt;price,
                  'sale' =&gt; $this-&gt;sale,
                  'tax' =&gt; $this-&gt;tax,
                  'paid_customer' =&gt; $this-&gt;paid_customer,
                  'remaining_customer' =&gt; $this-&gt;remaining_customer,
                  'paid_company' =&gt; $this-&gt;paid_company,
                  'remaining_company' =&gt; $this-&gt;remaining_company,
                  'r_invoice' =&gt; $this-&gt;r_invoice,
                  'deactivated_at' =&gt; $this-&gt;deactivated_at,
                  'isRenewed' =&gt; $this-&gt;isRenewed,
                  'created_at' =&gt; $this-&gt;created_at,
                  'updated_at' =&gt; $this-&gt;updated_at,
                  ]);
  
                  $query-&gt;andFilterWhere([ 'like', 'policy_type', $this-&gt;policy_type])
                          -&gt;andFilterWhere([ 'like', 'policy_code', $this-&gt;policy_code])-&gt;andFilterWhere(['like', 'madmoun_name', $this-&gt;madmoun_name])-&gt;andFilterWhere(['like', 'payment_option', $this-&gt;payment_option])-&gt;andFilterWhere(['like', 'attachment', $this-&gt;attachment])-&gt;andFilterWhere(['like', 'sale_letters', $this-&gt;sale_letters])-&gt;andFilterWhere(['like', 'paid_customer_letters', $this-&gt;paid_customer_letters])-&gt;andFilterWhere(['like', 'paid_company_letters', $this-&gt;paid_company_letters])-&gt;andFilterWhere(['like', 'table_name', $this-&gt;table_name]);
  
          $query-&gt;joinWith(['rCustomer' =&gt; function ($q) {
              $q-&gt;where('customers.first_name LIKE "%' . $this-&gt;customerName . '%"' .
                      'OR customers.fathers_name LIKE "%' . $this-&gt;customerName . '%"' .
                      'OR customers.last_name LIKE "%' . $this-&gt;customerName .  '%"'
              );
          }]);
  
  //        $query-&gt;joinWith(['rAvailableTaemin' =&gt; function ($q) {
  //                $q-&gt;where('companies.reprisentative_name LIKE "%' . $this-&gt;companyName . '%"');
  //            }]);
  //        $query-&gt;joinWith(['rCompany' =&gt; function ($q) {
                  //                $q-&gt;where('companies.name LIKE "%' . $this-&gt;companyName . '%"');
  //            }]);
  //        $query-&gt;joinWith(['companies' =&gt; function ($q) {
  //                $q-&gt;where('companies.name LIKE "%' . $this-&gt;companyName . '%"');
  //            }]);
  
                  return $dataProvider;
                  }
  
                  }
  </code></pre>
  
  <p>my model till now is as follows:</p>
  
  <pre><code>&lt;?php
  
  /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  
  namespace app\models\appmodels;
  
  use app\models\PoliciesGeneral;
  use Yii;
  
  /**
   * Description of AppPoliciesGeneral
   *
   * @author BigUser
   */
  class AppPoliciesGeneral extends PoliciesGeneral {
  
      public function getRCustomer() {
          return $this-&gt;hasOne(AppCustomers::className(), ['id' =&gt; 'r_customer_id']);
      }
  
      public function getCustomerName() {
  //        return $this-&gt;rCustomer-&gt;first_name . " " . $this-&gt;rCustomer-&gt;fathers_name . " " . $this-&gt;rCustomer-&gt;last_name . "";
          return $this-&gt;rCustomer-&gt;fullName;
      }
  
      public function getRAvailableTaemin() {
          return $this-&gt;hasOne(AppAvailableTaemin::className(), ['id' =&gt; 'r_available_taemin']);
      }
  
  //public function getRCompany() {
  //        return $this-&gt;hasMany(AppCompanies::className(), ['r_company' =&gt; 'id'])
  //                        -&gt;viaTable('available_taemin', ['r_available_taemin' =&gt; 'id']);
  //    }
  
      public function getCompanyid() {
          return $this-&gt;rAvailableTaemin-&gt;r_company;
      }
  
      public function getRCompany() {
          return $this-&gt;hasOne(AppCompanies::className(), ['id' =&gt; 'companyid']);
      }
  
      public function getCompanyName() {
          return $this-&gt;rCompany-&gt;name;
      }
  
  //    public function getCompanyName() {
  //        return $this-&gt;rAvailableTaemin-&gt;companyName;
  //    }
  
  
  }
  </code></pre>
  
  <p>THANKS IN ADVANCE.</p>