Codeigniter 3 博客应用程序错误:即使 post 的标题没有更改,post slug 也会更新

Codeigniter 3 blogging application bug: post slug updates even if there are no changes to the post's title

我正在使用 Codeigniter 3.1.8Bootstrap 4.[=20= 开发一个基本的博客应用程序]

对于此应用程序,post由 slug 唯一标识并显示给查看者。

我已经创建了一种机制,通过向 slugs 添加数字,防止重复的 slugs 如果有重复的 post 标题。此机制包含在 Posts 控制器的 create()update() 方法中。

update() 方法中,此机制运行不完善:即使 post 的标题没有更改,post slug 也会更新(my-post-title 变为 my-post-title-1 如果 "Update" 按钮被按下即使 id 没有重复的 post'标题)。

这里是 Posts 控制器中的 create() 方法。没有问题:

 public function create() {

    // Only logged in users can create posts
    if (!$this->session->userdata('is_logged_in')) {
        redirect('login');
    }

    $data = $this->get_data();
    $data['tagline'] = "Add New Post";

    if ($data['categories']) {
        foreach ($data['categories'] as &$category) {
            $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
        }
    }

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('desc', 'Short description', 'required');
    $this->form_validation->set_rules('body', 'Body', 'required');
    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');

    if($this->form_validation->run() === FALSE){
        $this->load->view('partials/header', $data);
        $this->load->view('dashboard/create-post');
        $this->load->view('partials/footer');
    } else {
        // Create slug (from title)
        $slug = url_title($this->input->post('title'), 'dash', TRUE);
        $slugcount = $this->Posts_model->slug_count($slug);
        if ($slugcount > 0) {
            $slug = $slug."-".$slugcount;
        }

        // Upload image
        $config['upload_path'] = './assets/img/posts';
        $config['allowed_types'] = 'jpg|png';
        $config['max_size'] = '2048';

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload()){
            $errors = array('error' => $this->upload->display_errors());
            $post_image = 'default.jpg';
        } else {
            $data = array('upload_data' => $this->upload->data());
            $post_image = $_FILES['userfile']['name'];
        }

        $this->Posts_model->create_post($post_image, $slug);
        $this->session->set_flashdata('post_created', 'Your post has been created');
        redirect('/');
    }
}

这是 Posts 控制器中的 update() 方法:

public function update() {
    // Form data validation rules
    $this->form_validation->set_rules('title', 'Title', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_rules('desc', 'Short description', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_rules('body', 'Body', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');

    $id = $this->input->post('id');

    // Update slug (from title)
    if ($this->form_validation->run()) {
        $slug = url_title($this->input->post('title'), 'dash', TRUE);
        $slugcount = $this->Posts_model->slug_count($slug);
        if ($slugcount > 0) {
            $slug = $slug."-".$slugcount;
        }
    } else {
        $slug = $this->input->post('slug');
    }

    // Upload image
    $config['upload_path'] = './assets/img/posts';
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '2048';

    $this->load->library('upload', $config);

    if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
        // Use name field in do_upload method
        if (!$this->upload->do_upload('userfile')) {
            $errors = array('error' => $this->upload->display_errors());

        } else {
            $data = $this->upload->data();
            $post_image = $data[ 'raw_name'].$data[ 'file_ext'];
        }
    }
    else {
        $post_image = $this->input->post('postimage');
    }

    if ($this->form_validation->run()) {
        $this->Posts_model->update_post($id, $post_image, $slug);
        $this->session->set_flashdata('post_updated', 'Your post has been updated');
        redirect('/' . $slug);
    } else {
        $this->form_validation->run();
        $this->session->set_flashdata('errors', validation_errors());
        redirect('/dashboard/posts/edit/' . $slug);
    }
}

Posts_model 模型中我有:

// Count the slugs in the posts table
public function slug_count($slug){
    $this->db->select('count(*) as slugcount');
    $this->db->from('posts');
    $this->db->where('slug', $slug);
    $query = $this->db->get();
    return $query->row(0)->slugcount;
}

// Update post
public function update_post($id, $post_image, $slug) {
    $data = [
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body'),
        'post_image' => $post_image,
        'cat_id' => $this->input->post('category'),
        'updated_at' => date('Y-m-d H:i:s')
    ];

    $this->db->where('id', $id);
    return $this->db->update('posts', $data);
}

我应该如何修改上面的代码来修复上面描述的错误?

// Update post
public function update_post($id, $post_image, $slug) {
    $data = [
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body'),
        'post_image' => $post_image,
        'cat_id' => $this->input->post('category'),
        'updated_at' => date('Y-m-d H:i:s')
    ];

    $this->db->where('id', $id);
    $query = $this->db->get('title');

    $count_row = $query->num_rows();

    if ($count_row > 0) {

        return FALSE; 
     } else {

        return $this->db->update('posts', $data); 
     }

}

这是因为它也在计算自己,如果更新,它总是大于 1,所以你需要添加另一个 where 语句(并传递 'id' 参数)

$this->db->where('id !=', $id);

之后的'slug_count'函数
$this->db->where('slug', $slug);

所以它不会自己计算

总结一下: 在 create() 方法中更改为:

$slugcount = $this->Posts_model->slug_count($slug, null);

update() 方法中更改为:

$slugcount = $this->Posts_model->slug_count($slug, $id);

这里是 slug_count:

// Count the slugs in the posts table
public function slug_count($slug, $id){
    $this->db->select('count(*) as slugcount');
    $this->db->from('posts');
    $this->db->where('slug', $slug);
    // if its an update
    if ($id != null) {
        $this->db->where('id !=', $id);
    }
    $query = $this->db->get();
    return $query->row(0)->slugcount;
}