In GridView :: widget I use 2 linked tables and therefore in 4 columns I need to write the value of the query to the adjacent table in the database as a variable:

$last_event = User::getLastEvent($model->id); 

Next in 'value' => function($model) {...} this column, I use $last_event instead of a query. But still, in the end, I get four duplicates of this request when the page loads, because It is used in four columns.

Question: Is it possible to set $last_event = User::getLastEvent($model->id); once in the table settings or somewhere else $last_event = User::getLastEvent($model->id); and use $last_event on all columns?

    1 answer 1

    You can locally cache the results in a property if you need to access them several times. For example, this option:

     public function getUserHash($id) { if(empty($this->userHash[$id])) { $this->userHash[$id] = uniqid(); } return $this->userHash[$id]; }