Handling MVC for multiple mysql tables

Hi all,

I am working with yii2 based web application. I have to display parent_name and child_name in view.

parent (pid, parent_name). pid is primary key.

child(cid, child_name, parent_id_from_parent_table). cid is primary key. parent_id_from_parent_table is a foreign key. These two are created in MySql.

Created CRUD/Model/Controller/View for both tables by using gii. Now, I can see both tables in view for those fields.

Now, I want to show parent_name, child_name list in some other view. How to handle this case? Any sample available?

Thanks in advance.

Hi, load Child model first,

$model = Child::find()->joinWith(['parent'])->all();

echo $model->child_name;

echo $model->parent->parent_name;

Here parent is child table hasOne relation

Hi Selva Kumar,

I am getting below error: Trying to get property of non-object while accessing attribute from model object.

And, How to iterate $model object and print in php html. Any idea?