M
M
Maxim2018-06-13 14:51:19
Yii
Maxim, 2018-06-13 14:51:19

Yii how to display related data in ActiveQuery?

Hello everyone)) Quite a simple question for connoisseurs, but I can’t figure out how to do it in ActiveQuery . In the model I display perfectly.
There is a table of events:
event
id | to_event | name
id - Event
ID to_event - event to which the event belongs (may not belong - NULL)
name - name of the event
and registration for master classes
RegMk
id | event_id
id - ID of the event
event_id - id of the event for which it is registered.
I need to output all registrations of my event + those that are "children" of this event (who has to_event == current event)
In ActiveQueryI display the registration of the current event

public function currentEvent($event_id)
    {
        return $this->andWhere(['' => $event_id])
    }

You need something like this:
public function currentEvent($event_id)
    {
        $this->andWhere(['' => $event_id]);
        
        $to_event = '?';//Нужно получить по связи
        //Если у текущего события есть значение to_event и оно равно текущему событию, то ищем такие записи
        if ($to_event && $to_event == $event_id) {
            $this->orWhere(['' => $to_event]);
        }   
        return $this;
    }

Using linked data in a RegMk model
/**
     * @return \yii\db\ActiveQuery
     */
    public function getToEvent()
    {
        return $this->hasOne(Event::className(), ['to_event' => 'event_id']);
    }

The bottom line is that when forming such a request, it is necessary to issue the registration of the event of the current event (1) and the registration of children
RegMk::find()->currentEvent(1)->all()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-06-13
@myks92

public function toEvent($event_id)
    {
        return $this->joinWith('toEvent')->andWhere(['event.to_event' => $event_id])
    }

C
Crash, 2018-06-13
@Bandicoot

Isn't it?

class Event extends yii\db\ActiveRecord
{
    // ...
    public function getEvents()
    {
        return $this->hasMany(Event::className(), ['to_event' => 'id']);
    }

    public function getRegs()
    {
        return $this->hasMany(RegMk::className(), ['event_id' => 'id']);
    }
    // ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question