【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
有一个核心函数,如下代码
public function get($id = NULL, $single = FALSE){ if ($id != NULL) { $filter = $this->_primary_filter; $id = $filter($id); $this->db->where($this->_primary_key, $id); $method = 'row'; } elseif($single == TRUE) { $method = 'row'; } else { $method = 'result'; } if (!count($this->db->ar_orderby)) { $this->db->order_by($this->_order_by); } return $this->db->get($this->_table_name)->$method(); }
这个函数想供多个Controller 使用,其中一个代码如下。
public function edit ($id = NULL) { if ($id) { $this->data['user'] = $this->user_m->get($id); count($this->data['user']) || $this->data['errors'][] = 'User could not be found'; } else { $this->data['user'] = $this->user_m->get_new(); } $rules = $this->user_m->rules; $id || $rules['password']['rules'] .= '|required'; $this->form_validation->set_rules($rules); if ($this->form_validation->run() == TRUE) { $data = $this->user_m->array_from_post(array( 'mobile', 'username', 'password', 'paytime', 'logintime', 'wechat', 'password', 'note')); $data['password'] = $this->user_m->hash($data['password']); $now = date('Y-m-d H:i:s'); $data['regtime'] = $now; $data['regip'] = $this->input->ip_address(); $this->user_m->save($data, $id); redirect('manage/user/'); } $this->data['subview'] = 'manage/user/edit'; $this->load->view('manage/_layout_main', $this->data); }
同一个Model下的编辑和修改操作使用的是同一个view,部分代码如下。
<?php echo empty($user->userid) ? '添加会员' : '修改会员' . $user->mobile . $user->username; ?> …………………… ………………………………………………
我的问题是:不同的Model的id字段名不一样,比如userid、aid、catid等等,这种情况下怎样使用上边那个核心函数,同时区分出来不同的Model的id字段,比如view中的userid。