title: Yii2: Access model value in view
body: <p>I have a variable status declared in model. Status is a column in gridview that is populated using this:</p>
  
  <pre><code>[
    'attribute' =&gt; 'status',
    'contentOptions' =&gt; ['class' =&gt; 'text-center'],
    'value' =&gt; function($model){
         if($model-&gt;nginSum != $model-&gt;sapValue){
           return 'NOK';
         } else {
            return 'OK';
         }
     },
  ],
  </code></pre>
  
  <p>I want to count the number os OKs and NOKs to display equal and diferent entries with a counter.</p>
  
  <p>I've tried declaring a function in search model:</p>
  
  <pre><code>public function getStatus()
  {
      return $this-&gt;status;
  }
  </code></pre>
  
  <p>and in view i did this:</p>
  
  <pre><code>$equal = 0;
  $different = 0;
  $stat = new EtuLojaSearch;
  if ($stat-&gt;getStatus() == 'OK') {
     $equal += 1;
  } else {
    $different += 1;
  }
  </code></pre>
  
  <p>but it's returning null.</p>
  
  <p>What am i doing wrong?</p>
  
  <p>Edited:
  function in model:</p>
  
  <pre><code>public function getStatus()
  {
      if($this-&gt;nginSum != $this-&gt;sapValue){
           return 'NOK';
         } else {
            return 'OK';
         }
  }
  </code></pre>
  
  <p>In view:</p>
  
  <pre><code>$iguais = 0;
  $diferentes = 0;
  $stat = new EtuLojaSearch;
  if ($stat-&gt;status == 'OK') {
      $iguais += 1;
  } else {
      $diferentes += 1;
  }
  </code></pre>
  
  <p>Edit 2:
  Status is ot getting bound to model using <code>getStatus()</code> function.</p>
  
  <p><a href="https://i.stack.imgur.com/X0UTu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X0UTu.png" alt="status value"></a></p>
  
  <p>Model</p>
  
  <pre><code>class EtuLoja extends \yii\db\ActiveRecord
  {
      public $nginSum;
      public $sapValue;
      public $status;
      public $Data_Sap;
  
  /**
   * {@inheritdoc}
   */
  public static function tableName()
  {
      return 'etu_loja';
  }
  
  public function getStatus()
  {
      if($this-&gt;nginSum != $this-&gt;sapValue){
           return 'NOK';
         } else {
            return 'OK';
         }
  }
  /**
   * {@inheritdoc}
   */
  public function rules()
  {
      return [
          [['id_master', 'nginSum', 'sapValue'], 'integer'],
          [['user'], 'required'],
          [['user', 'status'], 'string', 'max' =&gt; 100],
          [['Atendedora', 'Loja', 'Obs'], 'string', 'max' =&gt; 255],
          [['id_master'], 'exist', 'skipOnError' =&gt; true, 'targetClass' =&gt; MasterLoja::className(), 'targetAttribute' =&gt; ['id_master' =&gt; 'id']],
      ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function attributeLabels()
  {
      return [
          'ID' =&gt; 'ETU(Slave)',
          'id_master' =&gt; 'Master',
          'master.regiao' =&gt; 'Master/Região',
          'user' =&gt; 'User',
          'Atendedora' =&gt; 'Atendedora',
          'Loja' =&gt; 'Loja',
          'Obs' =&gt; 'Obs',
          'nginValue' =&gt; 'Valor Ngin',
          'status' =&gt; 'Estado',
      ];
  }
  
  /**
   * @return \yii\db\ActiveQuery
   */
  public function getMaster()
  {
      return $this-&gt;hasOne(MasterLoja::className(), ['id' =&gt; 'id_master']);
  }
  
  public function getNginAgentDetail()
  {
      return $this-&gt;hasMany(NginAgentDetail::className(),['CLIENT_ID' =&gt; 'ID']);        
  }
  
  public function getSap()
  {
      return $this-&gt;hasMany(Sap::className(),['Criado_por' =&gt; 'user']);        
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Search Model:</p>
  
  <pre><code>class EtuLojaSearch extends EtuLoja
  {
      public $regiao;
      public $nginValue;
      public $sapValue;
      public $operationDate;
      public $from_date;
      public $to_date;
      public $Data_Sap;
  
  public function rules()
  {
      return [
          [['ID'], 'integer'],
          [['Atendedora', 'Loja', 'Obs', 'id_master', 'regiao', 'user', 'nginValue', 'sapValue', 'operationDate', 'status', 'from_date', 'to_date', 'Data_Sap'], '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 = EtuLoja::find();
  
      // add conditions that should always apply here
      $query-&gt;joinWith('master');
      // $query-&gt;joinWith('nginAgentDetail');
      // $query-&gt;joinWith('sap');
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  
      $dataProvider-&gt;sort-&gt;attributes['Data_Sap'] = [
          'asc' =&gt; ['Data_Sap' =&gt; SORT_ASC],
          'desc' =&gt; ['Data_Sap' =&gt; SORT_DESC],
      ];
  
      $dataProvider-&gt;sort-&gt;attributes['regiao'] = [
          // The tables are the ones our relation are configured to
          // in my case they are prefixed with "tbl_"
          'asc' =&gt; ['master_loja.regiao' =&gt; SORT_ASC],
          'desc' =&gt; ['master_loja.regiao' =&gt; SORT_DESC],
      ];
  
      $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([
          'etu_loja.ID' =&gt; $this-&gt;ID,
          // 'id_master' =&gt; $this-&gt;id_master,
      ]);
  
      $query-&gt;andFilterWhere(['like', 'Atendedora', $this-&gt;Atendedora])
          -&gt;andFilterWhere(['like', 'Loja', $this-&gt;Loja])
          -&gt;andFilterWhere(['like', 'Obs', $this-&gt;Obs])
          -&gt;andFilterWhere(['like', 'user', $this-&gt;user]);
  
          if ($this-&gt;status === 'OK') {
              $query-&gt;andWhere('nginSum = sapValue');
          } elseif ($this-&gt;status === 'NOK') {
              $query-&gt;andWhere('nginSum &lt;&gt; sapValue');
          }
  
      if(" " !== $this-&gt;operationDate) {
          $query-&gt;andFilterWhere(['master_loja.regiao'=&gt;$this-&gt;regiao]);
      }
   return $dataProvider;
  }
  }
  </code></pre>