Search code examples
yii2active-record-query

Does Yii2 AR class can check, does two objects has a link?


In Kohana that was performed as $user->has('departments', array('id' => $dep->id_department))

I dont see the same in Yii2

$customer = Customer::findOne(123);

$order = new Order();
$order->save(); // now its id is - 33

$customer->link('orders', $order); // let think relation is via table

And now I want to check If my $customer HAS this object linked Order::find(33)


Solution

  • I found very ugly way to do this.

    $customer ->getOrders()->where(['id' => 33])->exists();
    

    Is there are shorter and pretty solutions, as its in Kohana does ?

    Kohana way:

    $customer ->has('orders', ['id' => 33])