M
M
MUTOgen4eg2015-11-07 15:52:35
Yii
MUTOgen4eg, 2015-11-07 15:52:35

How to work with cache fragments in Yii2?

Faced a strange problem. I am using fragment cache in Yii2.
Everything looks simple

if ($this->beginCache($key, ['dependency' => $dependency])) {...тут некоторые действия...}

I have this fragment in the cycle of passing through the models extracted from the database. Inside, a block of the same type is formed for each object. Reset condition - change the object itself
$dependency = [
 'class' => 'yii\caching\DbDependency',
 'sql' => 'SELECT updated FROM table WHERE id='.$model->id,
];

That is, the task is to accumulate a bunch of fragments for each object. Of course I make $key depending on the id of the model, like
"frag-".$model->id
But in the end I get strange nonsense. The display of such a page breaks. A simple view is output, without a template, and the output of the template begins immediately after the completion of this cycle. As soon as, for example, I make the cache id like this,
"frag"
i.e. the same for everyone, then I see the page in its normal form.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vyachin, 2016-06-13
@vyachin

$this->endCache(); forgot, read here https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question