多语言展示
当前在线:696今日阅读:19今日分享:20

phpcmsV9 2次开发如何新增模型关联其它模型文章

在做phpcms 2次开发的时候,需要新增模型。但是如何关联其它模型的文章呢?下面带你一步步修改吧!
工具/原料
1

需要修改的文件statics\js\content_addtop.js

2

需要修改的文件phpcms\modules\content\content.php

3

需要修改的文件phpcms\modules\content\templates\relationlist.tpl.php

方法/步骤
1

依次点击内容->模型管理->添加模型,模型名称和模型表键名按自己需要填写,其它不用修改。

2

找到刚才新增的模型,打开字段管理,添加自定义字段,字段类型选择万能字段,字段名可以自己取,接着修改系统默认的relation字段表单代码如下:

    其它默认即可。

    3

    打开网站根目录文件statics\js\content_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 += '

  • ·'+n.title+'
  • '; }); $('#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 += '

  • ·'+n.title+'
  • '; }); $('#'+fieldname+'_text').html(newrelation_ids);}); }

    6

    打开phpcms\modules\content\content.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

    打开phpcms\modules\content\templates\relationlist.tpl.php文件,修改: ',,'')' class='cu' title=''> categorys[$r['catid']]['catname'];?>

    9

    还有下面的js代码:

    10

    好了,大功告成!保存好后,新增文章选择刚才的模型,试试吧!

    注意事项

    源码更改后更新会覆盖的,要注意备份

    推荐信息