Answer the question
In order to leave comments, you need to log in
How to get an object with related data?
There are two tables. Categories and doctors. In order not to suffer a one-to-many relationship.
use yii\db\ActiveRecord;
class Category extends ActiveRecord
{
public static function tableName()
{
return 'category';
}
public function getDoc()
{
return $this->hasMany(Doctor::className(), ['category_id'=> 'id']);
}
}
use yii\db\ActiveRecord;
class Doctor extends ActiveRecord
{
public static function tableName()
{
return 'doctors';
}
public function getCategory()
{
return $this->hasOne(Category::className(), ['id'=> 'category_id']);
}
}
public function run()
{
$this->data=Category::find()->indexBy('id')->asArray()->all();
debug($this->data2);
return $this->tpl;
}
Answer the question
In order to leave comments, you need to log in
$this->data=Category::find()->with(['doc'])->indexBy('id')->asArray()->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question