1 - 下载 Swoole Loader 请下载 WINNT 系统 PHP-7.4.33 版本 非线程安全 的 swoole_loader 扩展,点击直达下载页面 2 - 安装 Swoole Loader 将刚才下载的 swoole_loader 扩展文件( swoole_loader.dll )上传到当前 PHP 的扩展安装目录中: ext......
phpcmsV9 2次开发如何新增模型关联其它模型文章
在做phpcms 2次开发的时候,需要新增模型。但是如何关联其它模型的文章呢?
下面带你一步步修改吧!
工具/原料
- 需要修改的文件staticsjscontent_addtop.js
- 需要修改的文件phpcmsmodulescontentcontent.php
- 需要修改的文件phpcmsmodulescontenttemplatesrelationlist.tpl.php
方法/步骤
依次点击内容->模型管理->添加模型,模型名称和模型表键名按自己需要填写,其它不用修改。
找到刚才新增的模型,打开字段管理,添加自定义字段,字段类型选择万能字段,字段名可以自己取,接着修改系统默认的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>
其它默认即可。
打开网站根目录文件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);}}
接着修改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);});}
新增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);});}
打开phpcmsmodulescontentcontent.php文件,修改:
public_relationlist函数,在$infos = $this->db->listinfo($where,'',$page,12);这句上面增加一句
$modelname=$_GET['modelname'];
新增函数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);}}
打开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 }?>
还有下面的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>
好了,大功告成!保存好后,新增文章选择刚才的模型,试试吧!
注意事项
- 源码更改后更新会覆盖的,要注意备份
以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!
标签: PHP
相关文章