I am trying to find a way to use a PHP variable inside sql query in Controller function and I don’t seem to get it right. In my case it is a variable that defines a date and I would need to use it in WHERE clause in several Controller functions.
Variable is defined in AppControllers initialize function, since I have understood that it is the place to define global variables. Is it so?
I have defined the date and then set it, like this: $this->set(‘defined_date’, $defined_date);
So what I’m looking for is something like this:
query = $this->Model->find();
$results = $query
->select([‘label’ => ‘model.column’])
->select([‘value’ => $query->func()->avg(‘model.column’)])
->where(['model.date>' => '$defined_date]) ->order(['value' => 'DESC']);
I get an error that $defined_date is not defined. How can I define it globally and is this the right way to use it in query?
E. I forgot to mention that $defined_date works in template so it is global, but how can I use it Controller?
E2. I just read about Components and created a Component that has a couple of functions and they return the dates I need. But the question remains: how do I use Component return values in query?
Many thanks! And sorry for this brain-smashing question flow…