我是codeigniter的新手。我为博客建立了一个带有codeigniter的cms。但是当我想要编辑文章帖子并想要保存它时,它会创建新的帖子,而不是更新我编辑的旧帖子。
请帮帮我。
我的文章编辑crontroller:
public function edit ($id = NULL)
{
// Fetch a article or set a new one
if ($id) {
$this->data['article'] = $this->article_m->get($id);
count($this->data['article']) || $this->data['errors'][] = 'article could not be found';
}
else {
$this->data['article'] = $this->article_m->get_new();
}
// categories for dropdown
$this->data['all_categories'] = $this->article_m->join();
// Set up the form
$rules = $this->article_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->article_m->array_from_post(array(
'title',
'extra_title',
'slug',
'image',
'category_id',
'body',
'pubdate'
));
$this->article_m->save($data, $id);
redirect('admin/article');
}
// Load the view
$this->data['subview'] = 'admin/article/edit';
$this->load->view('admin/_layout_main', $this->data);
}
我的模特:
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
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();
}
public function get_by($where, $single = FALSE){
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL){
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
// Update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
public function get_new ()
{
$article = new stdClass();
$article->title = '';
$article->extra_title = '';
$article->slug = '';
$article->image = '';
$article->category_id = '';
$article->body = '';
$article->pubdate = date('Y-m-d');
return $article;
}
public function join()
{
$this->db->select('name,categories.id as category_id');
$this->db->from($this->_table_name);
$this->db->join('categories', 'categories.id = category_id','right');
$Q = $this->db->get();
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['category_id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
我的观点:
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('admin/article/edit'); ?>
<table class="table">
<tr>
<td>Publication date</td>
<td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
</tr>
<tr>
<td>Category</td>
<td><?php
$js = 'id="category_id" onChange="some function();"';
echo form_dropdown('category_id', $all_categories, set_value('category_id', $article->category_id), $js); ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
</tr>
<tr>
<td>Extra Title (Optional)</td>
<td><?php echo form_input('extra_title', set_value('extra_title', $article->extra_title)); ?></td>
</tr>
<tr>
<td>Slug</td>
<td><?php echo form_input('slug', set_value('slug', $article->slug)); ?></td>
</tr>
<tr>
<td>Upload Image</td>
<td>
<div class="input-append">
<?php echo form_input('image', set_value('image', $article->image),'id="fieldID"'); ?>
<a href="http://localhost/cms/filemanager/dialog.php?type=1&field_id=fieldID" class="btn iframe-btn" type="button">Select</a>
</div>
</td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('body', set_value('body', $article->body), 'class="tinymce"'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
请帮助我。
答案 0 :(得分:1)
您必须将id传递给控制器。在您的视图中,您要告知要提交的表单 文章/编辑但不是id。
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('admin/article/edit/'.$article->id); ?>
...
如果未设置$ article,您可能会收到一些警告,因此您可能需要在添加新项目时进行一些检查。