I have a request to receive messages:
$messages = Yii::$app->getDb()->cache(function() use($dialogId, $offset, $limit) {
return Messages::find()
->innerJoinWith([
'author' => function($query) {
/* @var $query ActiveQuery */
$query->joinWith(['avatar']);
}
])
->where(['idDialog' => $dialogId])
->orderBy(['id' => SORT_DESC])
->offset($offset)
->limit($limit)
->asArray()
->all();
}, 0, new ChainedDependency([
'dependencies' => [
new DbDependency([
'sql' => (new Query())
->select("max(message.updateAt)")
->from([
'message' => Messages::find()
->select('updateAt')
->where(['idDialog' => $dialogId])
])
->createCommand()
->getRawSql(),
'reusable' => true,
]),
new ExpressionDependency([
'expression' => '$this->params["offset"]',
'params' => [
'offset' => $offset,
]
]),
new ExpressionDependency([
'expression' => '$this->params["dialogId"]',
'params' => [
'dialogId' => $dialogId,
]
])
]
]));
And there are three dependencies:
The messages have the relationship of the author, and the author has a relation to avatar.
Relation message to author:
Model Messages
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthor() {
return $this->hasOne(User::className(), ['id' => 'fromUser']);
}
Relation user to avatar:
Model User
/**
* @return \yii\db\ActiveQuery
*/
public function getAvatar() {
return $this->hasOne(Photo::className(), ['id' => 'avatar_id']);
}
With cache and dependency of main query of messages - all works good. But if I change something in the user tables or pictures, the main query produces the old data, obviously.
| Q | A
| ---------------- | ---
| Yii version | 2.0.11.2
| PHP version | 7.0.15-0ubuntu0.16.04.4
| Operating system | Ubuntu 16.04
Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.
We advise you to use our community driven resources:
If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.
_This is an automated comment, triggered by adding the label question._
@Igoreck did you ever get this question answered? I see no resources whatsoever anywhere about relation caching.
i would love to see if there are any docs about this
Most helpful comment
@Igoreck did you ever get this question answered? I see no resources whatsoever anywhere about relation caching.