今月初めて CodeIgniter 内で my_model と my_controller を操作しましたが、これでほとんど機能していると思います。
挿入機能が正常に動作するようになりました。ID があれば更新を追加しようとしています。
これが私のコードです:
function upsert_client($client_id = 0)
{
load_model('client_model');
$this->insertMethodJS();
$this->fields['client'] = $this->_prototype_client();
$user_id = get_user_id();
$company_id = get_company_id();
if ($client_id)
{
$this->data['client'] = $this->client_model->get_record($client_id);
}
if (!$this->ion_auth->in_group(GROUP_NAME_MANAGER, $user_id))
{
redirect('members/dashboard');
}
if ($_POST)
{
$this->load->helper('string');
if ($this->_validate_client())
{
$fields = $this->input->post(null , TRUE);
$fields['user_id'] = $user_id;
$fields['company_id'] = $company_id;
$fields['active'] = 1;
if ($client_id)
{
$fields['id'] = $this->client_model->get_record($client_id);
unset($fields['billing']);
$this->client_model->update($client_id, $fields);
}
else
{
unset($fields['billing']);
$this->client_model->insert($fields);
redirect('members/clients/manage_clients');
}
}
}
$this->template->write_view('content',$this->base_path.'/'.build_view_path(__METHOD__), $this->data);
$this->template->render();
}
function _prototype_client()
{
$fields = array();
$fields['id'] = 0;
$fields['name'] = '';
return $fields;
}
そして私のclient_modelから:
class Client_model extends MY_Model {
function get_record($client_id)
{
$query = $this->db->select('id')
->where(array('id'=>$client_id))
->get('clients');
return $query->row_array();
}
}
「クライアント」を編集しようとするたびに、新しいクライアントが挿入されます...現在編集しようとしているのは「名前」フィールドだけです。
私の編集ボタン:
<td><a href="add_client/<?= $client->id; ?>"><button class="btn btn-inverse" style="float: right;" type="button">Edit</button></a></td>
どんな助けでも大歓迎です、ありがとう!また、追加の詳細が必要な場合はお知らせください...