/** * 建立资源和标签之间的关联 * * $res_id * $res_type * $aAdded 标签对象的集合 * $aRemoved 标签对象的集合 */ public function save($mpid, $res_id, $res_type, $aAdded = null, $aRemoved = null) { $resTable = $this->resTable($res_type); /** * 建立关联 */ if (!empty($aAdded)) { foreach ($aAdded as $added) { if (!isset($added->id)) { /** * 标签是否已经存在? */ $q = array('id', 'xxt_tag', "mpid='{$mpid}' and title='{$added->title}'"); if (!($tag_id = $this->query_val_ss($q))) { /** * 不存在,创建新标签 */ $tag_id = $this->insert('xxt_tag', array('mpid' => $mpid, 'title' => $added->title), true); } $added->id = $tag_id; } if ('1' === parent::query_value('1', 'xxt_article_tag', "mpid='{$mpid}' and res_id={$res_id} and tag_id={$added->id}")) { // 关联已经存在 continue; } /** * 建立资源与标签之间的关联 */ $tag2res = array('mpid' => $mpid, 'res_id' => $res_id, 'tag_id' => $added->id); $this->insert('xxt_article_tag', $tag2res); } } /** * 删除关联 */ if (!empty($aRemoved)) { foreach ($aRemoved as $removed) { if (!isset($removed->id)) { /** * 没有指定标签的id * 查找标签是否存在,若不存在跳过 */ $q = array('id', 'xxt_tag', "mpid='{$mpid}' and title='{$removed->title}'"); if (!($removed->id = $this->query_val_ss($q))) { continue; } } /** * 删除资源与标签之间的关联 */ $this->delete('xxt_article_tag', "mpid='{$mpid}' and res_id={$res_id} and tag_id={$removed->id}"); } } return true; }