Answer the question
In order to leave comments, you need to log in
How to get data from related tables?
I have a problem, since I'm a beginner, I don't understand what goes where, I need to get data from the associated table, not the id, but the name of the employee in the otdel table
class Zakaz extends \yii\db\ActiveRecord
{
public function getIdSotrud($id)
{
return $this->hasOne(Otdel::className(), ['id_sotr' => 'id_sotrud']);
}
}
$sotrud = Zakaz::findOne('id_sotr');
$fio = $sotrud->fio
Answer the question
In order to leave comments, you need to log in
Links in Yii
Method getIdSotrud
needs to remove arguments$sotrud->IdSotrud->fio
There is a Zakaz table with an Odtel table associated with it, for this a connection is created in the Zakaz model:
public function getOtdel()
{
return $this->hasOne(Otdel::className(), ['otdel_id' => 'zakaz_id']);
//читать и понимать как: эта модель имеет связь один к одному с моделью Otdel. Где otdel_id модели Otdel равно zakaz_id модели Zakaz
}
$model = Zakaz::findOne($someid);
echo $model->otdel->fio; //понимать как: МодельZakazC ID = $someid обращаемся к связи, которая называется "otdel" и берем из нее атрибут fio
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question