首頁 > 網際網路

phpcmsV9 2次開發如何新增模型關聯其它模型文章

2019-12-14 01:02:15

在做phpcms 2次開發的時候,需要新增模型。但是如何關聯其它模型的文章呢?

下面帶你一步步修改吧!


1

依次點選內容->模型管理->新增模型,模型名稱和模型表鍵名按自己需要填寫,其它不用修改。


2

找到剛才新增的模型,開啟欄位管理,新增自定義欄位,欄位型別選擇萬能欄位,欄位名可以自己取,接著修改系統預設的relation欄位表單程式碼如下:

<input type='hidden' name='info[換成你的欄位名]' id='換成你的欄位名' value='{FIELD_VALUE}' style='50' ><ul class="list-dot" id="換成你的欄位名_text"></ul><div><input type='button' value="新增相關" onclick="omnipotent('selectid','?m=content&c=content&a=public_relationlist&modelid=1&modelname=換成你的欄位名','新增相關文章',1)" class="button" style="width:66px;"><span class="edit_content"><input type='button' value="顯示已有" onclick="show_myrelation(當前模型ID,目標模型ID,{ID},'換成你的欄位名')" class="button" style="width:66px;"></span></div>

其它預設即可。


3

開啟網站根目錄檔案staticsjscontent_addtop.js,修改remove_relation函數:

function remove_relation(sid,id,modelname) {var relation_ids = $('#'+modelname).val();if(relation_ids !='' ) {$('#'+sid).remove();var r_arr = relation_ids.split('|');var newrelation_ids = '';$.each(r_arr, function(i, n){if(n!=id) {if(i==0) {newrelation_ids = n;} else { newrelation_ids = newrelation_ids+'|'+n;}}});$('#'+modelname).val(newrelation_ids);}}


4

接著修改show_relation函數:

function show_relation(modelid,id,fieldname) {$.getJSON("?m=content&c=content&a=public_getjson_ids&modelid="+modelid+"&id="+id, function(json){var newrelation_ids = '';if(json==null) {alert('沒有新增相關文章');return false;}$.each(json, function(i, n){newrelation_ids += "<li id='"+n.sid+"'>·<span>"+n.title+"</span><a href='javascript:;' class='close' onclick="remove_relation('"+n.sid+"',"+n.id+",'"+fieldname+"')"></a></li>";});$('#relation_text').html(newrelation_ids);}); }


5

新增show_myrelation函數

function show_myrelation(modelid,modelid2,id,fieldname) {$.getJSON("?m=content&c=content&a=public_getjson_ids2&modelid="+modelid+"&modelid2="+modelid2+"&id="+id+"&fieldname="+fieldname, function(json){var newrelation_ids = '';if(json==null) {alert('沒有新增相關文章');return false;}$.each(json, function(i, n){newrelation_ids += "<li id='"+n.sid+"'>·<span>"+n.title+"</span><a href='javascript:;' class='close' onclick="remove_relation('"+n.sid+"',"+n.id+",'"+fieldname+"')"></a></li>";});$('#'+fieldname+'_text').html(newrelation_ids);}); }


6

開啟phpcmsmodulescontentcontent.php檔案,修改:

public_relationlist函數,在$infos = $this->db->listinfo($where,'',$page,12);這句上面增加一句

$modelname=$_GET['modelname'];


7

新增函數public_getjson_ids2:

public function public_getjson_ids2() {$modelid = intval($_GET['modelid']);$modelid2 = intval($_GET['modelid2']);$fieldname = $_GET['fieldname'];$id = intval($_GET['id']);$this->db->set_model($modelid);$tablename = $this->db->table_name;$this->db->table_name = $tablename.'_data';$r = $this->db->get_one(array('id'=>$id),$fieldname);if($r["{$fieldname}"]) {$myrelation = str_replace('|', ',', $r["{$fieldname}"]);$myrelation = trim($myrelation,',');$where = "id IN($myrelation)";$infos = array();$this->db->set_model($modelid2);$this->model = getcache('model', 'commons');$this->db->table_name = $this->db->db_tablepre.$this->model[$modelid2]['tablename'];//$this->db->table_name = $tablename;$datas = $this->db->select($where,'id,title');foreach($datas as $_v) {$_v['sid'] = 'v'.$_v['id'];if(strtolower(CHARSET)=='gbk') $_v['title'] = iconv('gbk', 'utf-8', $_v['title']);$infos[] = $_v;}echo json_encode($infos);}}


8

開啟phpcmsmodulescontenttemplatesrelationlist.tpl.php檔案,修改:

<?php foreach($infos as $r) { ?><tr onclick="select_list(this,'<?php echo safe_replace($r['title']);?>',<?php echo $r['id'];?>,'<?php echo $modelname;?>')" class="cu" title="<?php echo L('click_to_select');?>"><td align='left' ><?php echo $r['title'];?></td><td align='center'><?php echo $this->categorys[$r['catid']]['catname'];?></td><td align='center'><?php echo format::date($r['inputtime']);?></td></tr> <?php }?>


9

還有下面的js程式碼:

<SCRIPT LANGUAGE="JavaScript"><!--function select_list(obj,title,id,modelname) {var relation_ids = window.top.$('#'+modelname).val();var sid = 'v<?php echo $modelid;?>'+id;if($(obj).attr('class')=='line_ff9966' || $(obj).attr('class')==null) {$(obj).attr('class','line_fbffe4');window.top.$('#'+sid).remove();if(relation_ids !='' ) {var r_arr = relation_ids.split('|');var newrelation_ids = '';$.each(r_arr, function(i, n){if(n!=id) {if(i==0) {newrelation_ids = n;} else { newrelation_ids = newrelation_ids+'|'+n;}}});window.top.$('#'+modelname).val(newrelation_ids);}} else {$(obj).attr('class','line_ff9966');var str = "<li id='"+sid+"'><span>"+title+"</span><a href='javascript:;' class='close' onclick="remove_relation('"+sid+"',"+id+",'<?php echo $modelname;?>')"></a></li>";window.top.$('#'+modelname+'_text').append(str);if(relation_ids =='' ) {window.top.$('#'+modelname).val(id);} else {relation_ids = relation_ids+'|'+id;window.top.$('#'+modelname).val(relation_ids);}}}//--></SCRIPT>


10

好了,大功告成!儲存好後,新增文章選擇剛才的模型,試試吧!



IT145.com E-mail:sddin#qq.com