Yii2: How to solve the problem with caching relations of activerecord?

Created on 26 Mar 2017  路  3Comments  路  Source: yiisoft/yii2

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:

  1. Get last updated time of message
  2. Offset pages
  3. ID of dialog

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.

Question: How to define the dependency cache for relationships to the model or how can I refresh the cache for the relationships model

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.11.2
| PHP version | 7.0.15-0ubuntu0.16.04.4
| Operating system | Ubuntu 16.04

db question

Most helpful comment

@Igoreck did you ever get this question answered? I see no resources whatsoever anywhere about relation caching.

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kolyunya picture Kolyunya  路  3Comments

chaintng picture chaintng  路  3Comments

indicalabs picture indicalabs  路  3Comments

Locustv2 picture Locustv2  路  3Comments

kminooie picture kminooie  路  3Comments