Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

Yii2 model relation not working

Other model data not showing in my Gridview.

I have 2 models,ListForms and Lists. I want to show list name from Lists on listforms gridview. This is my code.

ListForms Model:

class ListForms extends \yii\db\ActiveRecord{

       // relation name
        public $listname;            

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'listForms';
    }     

  public function getListname() {   
      return $this->hasOne(Lists::classname(), ['listid' => 'listids']);                       
}

ListForms GridView:

[
 'label'=>'List Name',
 'attribute' => 'listname',
 'value' => 'listname.listname'
],

ListFormsSearch:

   public function search($params){                  
    $query = ListForms::find();
            $query->joinWith(['listname']);         
            //...           
    }

Upvotes: 1

Views: 2244

Answers (1)

Mihai P.
Mihai P.

Reputation: 9367

Remove

public $listname;       

from the model, because you declare the variable in the model it will NOT be taken from the database and shown. Basically it will not work in a lot of places.

Upvotes: 3

Related Questions