Hi there
How can i use/enable cache in my relations?
thanks
Hi there
How can i use/enable cache in my relations?
thanks
any help?
I assume that you have read the guide about caching ?
If you have, then I am sure that you are able to write a more detailed question.
read theme beofre post, i dont know how use in realtions
but tried to use fragment in view, my css between fragment destroyed
You have to be more specific. We have no clue other than "cache" and "relation".
This is what we see in the Guide - Data Caching - Query Caching
So, did you try to do something like the following?
$result = Customer::getDb()->cache(function ($db) {
return Customer::find()->where(['id' => 1])->with(['something'])->one();
});
You mean Fragment Caching?
What’s that?
Yes
I use Fragment Caching in my view, in that fragment there are some inline css styles and html tags too
the view of that piece of code not load properly.
Please show your code.
Are you deliberately avoiding to post any solid information regarding your problem !! ?
Give us some [size="5"][b]code
[/b][size=“2”]Show us what you have tried. Please [/size][b]
[/b][/size]
So sorry I thought its obvious 'cause Im talking about relations.
This is that piece of view:
...
<?php
$dependency = [
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT MAX(updated_at) FROM question WHERE status = 1',
];
if ($this->beginCache($exam->id, ['dependency' => $dependency])) {
foreach ($exam->questions as $question) {
echo '<li>';
QuestionController::actionQuestionPreview($question->id, $question->type, $question->title, $question->content, $question->helper, true);
echo '</li>';
echo Html::tag('hr');
}
$this->endCache();
}
?>
...
This is the question(many)-(one)exam relation:
public function getQuestions()
{
return $this->hasMany(Question::className(), ['exam_id' => 'id'])->where('question.version = :version AND question.status = :status', [
':version' => Question::VERSION_NEW,
':status' => Question::STATUS_ACTIVE
])->orderBy('priority ASC');
}
And this is that static method in QuestionController(Its content is not important, only check the structure and generated tags):
public static function actionQuestionPreview($id, $type, $title, $content, $helper, $shuffle = false)
{
echo Html::tag('h4', Html::encode($title));
switch ($type) {
case(Question::TYPE_BLANK):
preg_match_all(Question::REGEX_SIMPLE_SPLIT, Html::encode($content), $matches);
$input_array_index = array_keys($matches[0]);
sort($matches[0]);
$option_tag = Html::tag('option', '', ['value' => '']);
foreach ($matches[0] as $match) {
$value = str_replace([Question::NO_REGEX_SIMPLE_SPLIT_LEFT_SIDE, Question::NO_REGEX_SIMPLE_SPLIT_RIGHT_SIDE], ['', ''], $match);
$option_tag .= Html::tag('option', $value, ['value' => $value, 'style' => 'padding: 5px']);
}
$preview = preg_replace_callback(Question::REGEX_SIMPLE_SPLIT, function () use (&$input_array_index,$id, $option_tag) {
return Html::tag('select', $option_tag, [
'style' => 'border:1px solid black; border-radius: 5px; padding: 0px; background-color: transparent',
'name' => "Q[{$id}][" . array_shift($input_array_index) . "]",
]);
}, Html::encode($content));
echo Html::tag('p', nl2br($preview), ['class' => 'line-space']);
break;
case(Question::TYPE_DRAGGABLE):
$preview = Html::encode($content);
//grap all items
preg_match_all(Question::REGEX_SIMPLE_SPLIT, $preview, $matches);
//remove special chars by using constants
foreach ($matches[0] as $key => $value) {
$matches[0][$key] = str_replace([Question::NO_REGEX_SIMPLE_SPLIT_LEFT_SIDE, Question::NO_REGEX_SIMPLE_SPLIT_RIGHT_SIDE], ['', ''], $value);
}
$helper = nl2br(Html::encode($helper));
$helper = str_replace('<br/>', '<br />', $helper);
$helper = explode('<br />', $helper);
$helper = array_filter($helper);
//merge items with extra items
$options = array_merge($matches[0], $helper);
if ($shuffle == true)
shuffle($options);
echo '<div class="draggable-parent">';
foreach ($options as $key => $value) {
echo '<span style="margin: 1px;" id=' . $id . '_' . $key . ' class="btn btn-warning draggable">' . $value . '</span> ';
}
echo '<br/>';
echo '<br/>';
preg_match_all(Question::REGEX_SIMPLE_SPLIT, Html::encode($content), $matches);
$input_array_index = array_keys($matches[0]);
$preview = preg_replace_callback(Question::REGEX_SIMPLE_SPLIT, function () use (&$input_array_index, $id) {
$input_tag = '<input type="hidden" value="" name="Q[' . $id . '][' . array_shift($input_array_index) . ']"/>';
$span_tag = '<span style="display: inline-block;"><span data-dragstatus="0" style="margin-bottom: 2px;" class=" dropzone btn drag-place">Drop Here and Fill Me</span></span>';
return $input_tag . $span_tag;
}, Html::encode($content));
echo Html::tag('p', nl2br($preview));
echo '</div>'; //end of div.draggable-parent
break;
case(Question::TYPE_CHECKABLE):
echo Html::tag('p', nl2br(Html::encode($content)));
$helper = nl2br(Html::encode($helper));
$helper = str_replace('<br/>', '<br />', $helper);
$options = explode('<br />', $helper);
$options = array_filter($options);
array_walk($options, function (&$value) {
$value = str_replace([Question::NO_REGEX_SIMPLE_SPLIT_LEFT_SIDE, Question::NO_REGEX_SIMPLE_SPLIT_RIGHT_SIDE], ['', ''], $value);
});
//shuffle associate
$keys = array_keys($options);
if ($shuffle == true)
shuffle($keys);
$random_options = [];
foreach ($keys as $key)
$random_options[$key] = $options[$key];
echo '<ol>';
foreach ($random_options as $key => $value) {
echo '<li>';
echo '<div class="checkbox">';
echo '<label>';
echo '<input name="Q[' . $id . '][' . $key . ']" type="checkbox" value="selected"/>' . $value;
echo '</label>';
echo '</div>';
echo '</li>';
}
echo '</ol>';
break;
}
}
I would prefix "$exam->id" with some string like "exam-" in order to make sure the id of the cache will be unique.
if ($this->beginCache("exam-" . $exam->id, ['dependency' => $dependency])) {
...
But except for it I don’t see anything strange in your code so far.
good idea but i sure its unique at least for now
Yes there is nothing strange but i dont know why my css codes didnt work fine.
maybe because in "QuestionController::actionQuestionPreview()" method there are some css and i call it in cache section.
No, I don’t think so. Maybe some cause other than cache.
I dont know what was the problem but now it works like a charm!