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

67 Views Asked by At

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)

1

There are 1 best solutions below

0
YanAlex On BEST ANSWER

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])