Example #1
0
 public function anchor($view_file, $hash = '')
 {
     $model_id = Json::encode($this->owner->primaryKey);
     /* @var LinkAnchorBinding $anchoreModel */
     $returnAnchor = \Yii::$app->getCache()->get("Anchor::{$this->owner->className()}::{$model_id}::{$view_file}::{$hash}");
     if (!$returnAnchor) {
         $anchorModel = LinkAnchorBinding::find()->with(['index'])->where(['and', [LinkAnchorBinding::tableName() . '.model_name' => $this->owner->className(), LinkAnchorBinding::tableName() . '.model_id' => $model_id, LinkAnchorBinding::tableName() . '.view_file' => $view_file, LinkAnchorBinding::tableName() . '.params_hash' => $hash]])->one();
         if ($anchorModel === null) {
             /* @var ModelAnchorIndex $indexModel */
             $indexModel = $this->lastIndex;
             if ($indexModel === null) {
                 $indexModel = new ModelAnchorIndex(['model_name' => $this->owner->className(), 'model_id' => $model_id, 'next_index' => 0]);
             }
             $anchors = LinkAnchor::find()->where(['and', ['model_name' => $this->owner->className(), 'model_id' => $model_id]])->orderBy([LinkAnchor::tableName() . '.sort_order' => SORT_ASC])->limit((int) $indexModel->next_index + 1)->all();
             if (!empty($anchors)) {
                 if (!isset($anchors[$indexModel->next_index])) {
                     $indexModel->next_index = 0;
                 }
                 $anchor = $anchors[$indexModel->next_index];
                 $indexModel->next_index++;
                 $indexModel->save();
                 $bind = new LinkAnchorBinding(['link_anchor_id' => $anchor->id, 'view_file' => $view_file, 'params_hash' => $hash, 'model_name' => $this->owner->className(), 'model_id' => $model_id]);
                 $bind->save();
                 $returnAnchor = $anchor->anchor;
             } else {
                 throw new NotFoundHttpException();
             }
         } else {
             $returnAnchor = $anchorModel->anchor->anchor;
         }
         \Yii::$app->getCache()->set("Anchor::{$this->owner->className()}::{$model_id}::{$view_file}::{$hash}", $returnAnchor, $this->cache_expire);
     }
     return $returnAnchor;
 }