How to pass a variable into a callback in php with Yii2

0

I'm trying to make a rather abstract method that would execute a yii2 query depending on the attribute passed my code is:

public static function getModelosQuePodemTer($atributo) {
    self::$helper = $atributo;
    return self::find()
                ->joinWith(['modelo' => function($query) {
                        $query->andWhere([self::$helper => 1]);
                    }])
                ->all();

If I'm not mistaken, I can not pass another attribute on the function being called, and I think my solution is a gambiarra

I wonder if there is another way to do this

    
asked by anonymous 28.02.2018 / 15:51

1 answer

1

You can use use to access% $atributo within the scope of the function.

public static function getModelosQuePodemTer($atributo) {

    return self::find()
                ->joinWith(['modelo' => function($query)use($atributo) {
                        $query->andWhere([$atributo=> 1]);
                    }])
                ->all();
    
28.02.2018 / 16:03