O
O
Oleg Aksenov2015-11-16 12:00:03
Yii
Oleg Aksenov, 2015-11-16 12:00:03

How to create a filter using hasMany and hasOne?

There are, for example, such linking tables with Ticket

public function getGroup()
    {
        return $this->hasOne(Group::className(), ['id' => 'group_id'])
      ->viaTable('{{%group_ticket}}', ['ticket_id'=>'id'])->where(['id'=>
      GroupTicket::find()->select('group_id')->where(['id'=>
      GroupTicket::find()->select('max(id)')->groupBy('ticket_id')
      ])

      ]);
    }

and
public function getGroupTickets()
    {
        return $this->hasMany(GroupTicket::className(), ['ticket_id' => 'id']);
    }

How to correctly register a filter in TicketSearch, is it more accurate to specify related tables?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Kudinov, 2015-11-16
@Takun

$query = Ticket::find()->joinWith(['group', 'groupTickets']);
$query->where([
    'group.name' => $groupName, 
    'groupTickets.name' => $ticketName,
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question