Add parameters to joinWith anonymous function

Hi there,

is there a way to add parameters to the anonymous function used in with() or joinWith() relations ?

For example, in the documentation we have :


// SELECT `customer`.* FROM `customer`

// LEFT JOIN `order` ON `order`.`customer_id` = `customer`.`id` AND `order`.`status` = 1 

// 

// SELECT * FROM `order` WHERE `customer_id` IN (...)

$customers = Customer::find()->joinWith([

    'orders' => function ($query) {

        $query->onCondition(['order.status' => Order::STATUS_ACTIVE]);

    },

])->all();

What if i want to get all orders with amount = 50 ?




$amount = 50;

$customers = Customer::find()->joinWith([

    'orders' => function ($query) {

        $query->onCondition(['order.amount' => $amount]);

    },

])->all();

In the anonymous function, $amount is obviously undefined, is there a way to solve that ?

Thanks !

Hi!

Not completly sure but you could try this:




$amount = 50;

$customers = Customer::find()->joinWith([

    'orders' => function ($query, $amount) {

        $query->onCondition(['order.amount' => $amount]);

    },

])->all();

Regards

Thanks, but i already tried and it fails ::) :


Missing argument 2 for frontend\\controllers\\MyController::frontend\\controllers\\{closure}()

http://www.yiiframework.com/forum/index.php/topic/64370-joinwith-anonimous-function-second-arg/

http://www.yiiframework.com/forum/index.php/topic/63964-how-to-pass-variable-into-joinwith-anonymous-function/

Nice, thanks !

(what did i do not to find these topics ?!!)