H
H
hollanditkzn2017-03-15 16:35:09
Yii
hollanditkzn, 2017-03-15 16:35:09

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']);
    }
}

And I need to display it somewhere, but I don’t know where because I am new to yii2
$sotrud = Zakaz::findOne('id_sotr');
$fio = $sotrud->fio

If you need a complete picture of the files
https://github.com/hollandit/crm.git

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Derepko, 2017-03-15
@hollanditkzn

Links in Yii
Method getIdSotrudneeds to remove arguments
$sotrud->IdSotrud->fio

M
Maxim Timofeev, 2017-03-15
@webinar

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
    }

Next, let's say in the controller, you can find an order and access the attributes of the department associated with it:
$model = Zakaz::findOne($someid);
echo $model->otdel->fio; //понимать как: МодельZakazC ID = $someid обращаемся к связи, которая называется "otdel" и берем из нее атрибут fio

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question