/**
?????*?添加標(biāo)簽方法
?????*?param:$event為存儲傳入的tag數(shù)組值
?????*/
????public?function?_eventAddTag($event)
????{
????????//實例化表單模型
????????$tag?=?new?TagForm();
????????//傳值
????????$tag->tags?=?$event->data['tags'];
????????//實現(xiàn)保存方法
????????$tagids?=?$tag->saveTags();
????????//刪除原先的關(guān)聯(lián)關(guān)系,與文章多對多的關(guān)系,利用數(shù)據(jù)庫表中的relation_post_tags的對應(yīng)關(guān)系
????????RelationPostTags::deleteAll(['post_id'?=>?$event->data['id']]);
????????//遍歷多維數(shù)據(jù)
????????if?(!empty($tagids))?{
????????????foreach?($tagids?as?$k=>$id){
????????????????$row[$k]['post_id']?=?$this->id;
????????????????$row[$k]['tag_id']?=?$id;
????????????}
????????????//實現(xiàn)批量插入保存
????????????$res?=?(new?Query())->createCommand()
????????????->batchInsert(RelationPostTags::tableName(),['post_id','tag_id'],$row)
????????????->execute();
????????????//保存失敗
????????????if?($res)
????????????????throw?new?\Exception("關(guān)聯(lián)保存失敗");
????????}
????}
<?php
namespace?frontend\models;
use?Yii;
use?common\models\Tags;
use?yii\base\Model;
/**
?*?標(biāo)簽表單模型
?*?@author?ZJJ
?*
?*/
class?TagForm?extends?Model
{
????//定義參數(shù)屬性
????public?$id;
????public?$tags;
????/**
?????*?加入規(guī)則
?????*/
????public?function?rules()
????{
????????return?[
????????????['tags','required'],
????????????['tags','each','rule'=>['string']],
????????];
????}
????/**
?????*?保存標(biāo)簽集合
?????*?返回所有的id
?????*/
????public?function?saveTags()
????{
????????//定義常量ids
????????$ids?=?[];
????????if?(!empty($this->tags))?{
????????????foreach?($this->tags?as?$tag){
????????????????$ids[]?=?$this->_saveTag($tag);
????????????}
????????}
????????return?$ids;
????}
????/**
?????*?保存單個標(biāo)簽的方法
?????*/
????private?function?_saveTag($tag)
????{
????????//利用業(yè)務(wù)邏輯
????????$model?=?new?Tags();
????????//查詢標(biāo)簽是否存在
????????$res?=?$model->find()->where(['tag_name'=>$tag])->one();
????????//新建標(biāo)簽
????????if?(!$res)?{
????????????//數(shù)據(jù)保存,與數(shù)據(jù)表中tags字段保持一致
????????????$model->tag_name?=?$tag;
????????????$model->post_num?=?1;
????????????if?(!$model->save()){
????????????????throw?new?\Exception("保存標(biāo)簽失敗");
????????????}
????????????return?$model->id;
????????}else?{
????????????//post_num字段的值基礎(chǔ)上+1
????????????$res->updateCounters(['post_num'?=>?1]);
????????}
????????return?$res->id;
????}
}
?public?function?behaviors()
????{
????????return?[
????????????'access'?=>?[
????????????????'class'?=>?AccessControl::className(),
????????????????'only'?=>?['index',?'create',?'upload',?'ueditor'],
????????????????'rules'?=>?[
????????????????????[
????????????????????????'actions'?=>?['index'],
????????????????????????'allow'?=>?true,
????????????????????],
????????????????????[
????????????????????????'actions'?=>?['create',?'upload',?'ueditor'],
????????????????????????'allow'?=>?true,
????????????????????????'roles'?=>?['@'],
????????????????????],
????????????????],
????????????],
????????????'verbs'?=>?[
????????????????'class'?=>?VerbFilter::className(),
????????????????'actions'?=>?[
????????????????????'*'?=>?['get',?'post'],
????????????????????'create'?=>?['get','post'],
????????????????],
????????????],
????????];
????}
2017-07-03
if?($res)
????????????????throw?new?\Exception("關(guān)聯(lián)保存失敗");
因該是if?(!$res), 錯誤才返回異常