Getting unknown property: after adding a new relation

Hi all. Been here trying to get to grips with Yii for a project I am working on. Basically I am getting an unknown property error after adding a relation to a model, to pull data from another table. Spent all day trying to find answer to this and finally am asking for help…

I have a model Termclassstudent (model for a join table) which i am displaying on an input table for the parent student table.

I created relations in the Termclassstudent model to link back to related tables. When I add the final relation (with the data I want to display), and add the joinwith to my query in the view - i get the following error.

Getting unknown property: app\models\Termclassstudent::classid

model below


<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "termclassstudent".

 *

 * @property integer $id

 * @property integer $termclassid

 * @property integer $studentid

 *

 * @property Termclass $termclass

 * @property Student $student

 */

class Termclassstudent extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'termclassstudent';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['termclassid', 'studentid'], 'integer']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'termclassid' => 'Termclassid',

            'studentid' => 'Studentid',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getTermclass()

    {

        return $this->hasOne(Termclass::className(), ['id' => 'termclassid']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getStudent()

    {

        return $this->hasOne(Student::className(), ['id' => 'studentid']);

    }

    

     public function getClasse()

    {

        return $this->hasOne(Classe::className(), ['id' => 'classid'])

                ->via('termclass');

    }

    

    public function getClassday()

    {

        return $this->hasone(Classday::className(), ['id' => 'dayid'])

                ->via('classe');

    }

}



and the offending view code :


<div class="student-form">


    <?php $form = ActiveForm::begin(); ?>

    <?php if (!$model->isNewRecord):?>

    <h2> Classes </h2>

    <?= \yii\grid\GridView::widget([

        'dataProvider' => new \yii\data\ActiveDataProvider ([

            'query' => $model->getTermclassstudents()->joinWith('termclass')->joinWith('classe')->joinWith('classday'),

            'pagination' => false

        ]),

        'columns' => ['classday.name','classe.name','classe.time',

        [

            'class' => \yii\grid\ActionColumn::className(),

            'controller' => 'termclassstudent',

            'header' => Html::a('<i class="glyphicon glyphicon-plus"></i>&nbsp;Add New', ['termclassstudent/create','relation_id'=>$model->id]),

            

        ]

        ]

    ]);

    ?>

    <?php endif?>

it all works till I add the final joinWith(‘classday’) to my query. classid, which the error refers to is in the Termclass model.

I can’t see what is wrong, retyped, regenerated etc etc. I could be missing something obvious or misunderstanding how the relation functions, but what??

UPDATE:- I see that if I create the smae relations in other models which are related to termclassstudent, I can use the classday relation from above fine. Only seems to be a problem with the termclassstudent model, but I can’t see what…

‘hasOne’ instead of ‘hasone’, maybe.

Hi, thanks for the reply. that was just a typo when I was fixing it for upload - messed around with that file so many times yesterday. Didn’t make any difference… However, I finally solved late last night how to do what I wanted to do and maybe what I did with the joinWiths was unecessaary, although still bemused as to why it doesn’t work, as the sql looks fine.

Basically, my query field now reads


'query' => $model->getTermclassstudents()->joinWith('termclass.class.classday'),

rather than


'query' => $model->getTermclassstudents()->joinWith('termclass')->joinWith('classe')->joinWith('classday'),

So it is using the existing relations that I have declared in each model to its related model, without me having to declare them all in the base model I am using (termclassstudents in this case). Which makes a lot of sense, just didn’t see any documentation using this example, just a bit of trial and error.

I see that using VIA in a relation is usually (only?) for link tables and so possibly I used them incorrectly in my original relations.

Hope this helps someone else anyway!

I see.

The dot notation for nested relation (‘termclass.class.classday’) is described in the API doc of “with”, but is missing from that of “joinWith” and alikes.

And I also thought that the original source of yours should work as expected. But one thing I noticed is that there’s a difference between ‘getClasse()’ and ‘getClassday()’, although they share the same approach using ‘via’.




    public function getTermclass()

    {

        return $this->hasOne(Termclass::className(), ['id' => 'termclassid']);

    }


     public function getClasse()

    {

        return $this->hasOne(Classe::className(), ['id' => 'classid'])

                ->via('termclass');

    }

    

    public function getClassday()

    {

        return $this->hasOne(Classday::className(), ['id' => 'dayid'])

                ->via('classe');

    }



‘termclass’ relation used in getClasse() is a normal relation without ‘via’, while the ‘classe’ relation used in getClassday() is not. It is a kind of nested relation itself using ‘via’.

I suppose that it might be the reason why Yii couldn’t resolve the getClassday() relation.