A
A
AlexSer2018-01-31 13:43:11
Yii
AlexSer, 2018-01-31 13:43:11

Yii2 how to insert parameters into a function?

Still found how to filter. Here is the code

$query=Napravlenie::find()->joinWith(['pacients' => function ($q) {
                         $q->where('pacients.dr="1990-02-25"');
                     }]);

I need to filter out all who are 18 years old.
$yearOfBirth = 18;
                     $date1 = date((date('Y') - $yearOfBirth - 1) . '-m-d');
                     $date2 = date((date('Y') - $yearOfBirth) . '-m-d');
                     $query=Napravlenie::find()->joinWith(['pacients' => function ($q, $date1, $date2) {
                         $q->where("pacients.dr BETWEEN $date1 AND $date2");
                     }]);

The function starts complaining about the $date1, $date2 parameters. Tell me how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2018-01-31
@AlexSer

$yearOfBirth = 18;
$date1 = date((date('Y') - $yearOfBirth - 1) . '-m-d');
$date2 = date((date('Y') - $yearOfBirth) . '-m-d');
$query=Napravlenie::find()->joinWith(['pacients' => function ($q) use ( $date1, $date2) {
   $q->where("pacients.dr BETWEEN :d1 AND :d2",["d1"=>$date1,"d2"=>$date2]);
}]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question