Yii Framework Forum: Activerecord Eager Loading - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Activerecord Eager Loading

#1 User is offline   rusk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-March 14

Posted 04 August 2014 - 04:24 AM

From official guide Docs
class Customer extends \yii\db\ActiveRecord
{
    public function getBigOrders($threshold = 100)
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])
            ->where('subtotal > :threshold', [':threshold' => $threshold])
            ->orderBy('id');
    }
}


How can I achieve the following:
$customers = Customer::find()
  ->with('bigOrders') //here I need to provide $threshold somehow
  ->all();

0

#2 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 02-January 10

Posted 04 August 2014 - 06:55 AM

Maybe...

$customers = Customer::find()
  ->with(['bigOrders'=>function($query){
   $query->andWhere('threshold > :threshold',[
   ':threshold'=>$threshold
]);
}])
  ->all();

0

#3 User is offline   rusk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-March 14

Posted 04 August 2014 - 09:18 PM

I understand how to achieve the result, but I need it without code duplicate (Imagine if there is complicated query and I need it in few places).
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users