Answer the question
In order to leave comments, you need to log in
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])
}
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;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getToEvent()
{
return $this->hasOne(Event::className(), ['to_event' => 'event_id']);
}
RegMk::find()->currentEvent(1)->all()
Answer the question
In order to leave comments, you need to log in
public function toEvent($event_id)
{
return $this->joinWith('toEvent')->andWhere(['event.to_event' => $event_id])
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question