Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TagBehavior的afterSave,没有考虑更新时完全删除tag的情况 #25

Closed
dakcom opened this issue Mar 23, 2017 · 1 comment
Closed

Comments

@dakcom
Copy link
Contributor

dakcom commented Mar 23, 2017

RT。图省事直接加了
if(isset($data[static::$formName]) && empty($data[static::$formName])) {
if(!$this->owner->isNewRecord) {
$this->beforeDelete();
}
}
如果规范,是否加个if嵌套?

@yidashi
Copy link
Owner

yidashi commented Mar 23, 2017

恩。

public function afterSave()
    {
        if (\Yii::$app->request->isConsoleRequest ) {
            return;
        }
        $data = \Yii::$app->request->post($this->owner->formName());
        if(isset($data[static::$formName])) {
            if(!$this->owner->isNewRecord) {
                $this->beforeDelete();
            }
            if (!empty($data[static::$formName])) {
                $tags = $data[static::$formName];
                foreach ($tags as $tag) {
                    $tagModel = Tag::findOne(['name' => $tag]);
                    if (empty($tagModel)) {
                        $tagModel = new Tag();
                        $tagModel->name = $tag;
                        $tagModel->save();
                    }
                    $articleTag = new ArticleTag();
                    $articleTag->article_id = $this->owner->id;
                    $articleTag->tag_id = $tagModel->id;
                    $articleTag->save();
                }
                Tag::updateAllCounters(['article' => 1], ['name' => $tags]);
            }
        }
    }

@yidashi yidashi closed this as completed Mar 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants