在数据库CodeIgniter

时间:2018-03-16 22:05:06

标签: php codeigniter codeigniter-3

我目前有一种方法可以将新数据发布到数据库中,并且效果很好,当我尝试将当前id添加到ci_relationship表时会出现问题。

这是我的方法:

public function add()
{
    // Field Rules
    $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[3]');
    $this->form_validation->set_rules('body', 'Body', 'trim|required');
    $this->form_validation->set_rules('status', 'Status', 'required');
    $this->form_validation->set_rules('is_featured', 'Feature', 'required');
    $this->form_validation->set_rules('is_commented', 'Comments', 'required');
    $this->form_validation->set_rules('order', 'Order', 'integer');

    if ($this->form_validation->run() == FALSE) {

        // Select Categories    
        $categories_options = array();
        $categories_options[0] = 'Select Categories';

        $categories_list = $this->Terms_model->get_list();

        foreach($categories_list as $cat){
            $categories_options[$cat->term_id] = $cat->title;
        }

        $data['categories_options'] = $categories_options;

        // Select Post Author ID and Name
        $user_options = array();
        $user_options[0] = 'Select Username ID';

        $user_list = $this->User_model->get_list();

        foreach($user_list as $username){
            $user_options[$username->id] = $username->username;
        }

        $data['user_options'] = $user_options;

        // Load template
        $this->template->load('admin', 'default', 'posts/add', $data);
    } else {
        $slug = str_replace(' ', '-', $this->input->post('title'));
        $slug = strtolower($slug);

        // Upload Image
        $config['upload_path'] = 'assets/img/posts/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = '2048';
        $config['max_width'] = '2000';
        $config['max_height'] = '2000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);


        $file_name = 'post_image';

        if(!$this->upload->do_upload($file_name)){
            varDebug($this->upload->display_errors());
            $file_name = 'assets/img/noimage.jpg';
        } else {
            $post_image =  $this->upload->data('file_name');
        }

        // Page Data
        $data = array(
            'user_id'       => $this->input->post('user_id'),
            'slug'          => $slug,
            'title'         => $this->input->post('title'),
            'post_image'    => $post_image,
            'body'          => $this->input->post('body'),
            'status'        => $this->input->post('status'),
            'is_featured'   => $this->input->post('is_featured'),
            'is_commented'  => $this->input->post('is_commented'),
            'order'         => $this->input->post('order'),
            'type'          => 'post',
        );

        // Insert Page
        $this->Post_model->add($data);

        $post_id = $this->db->insert_id();

        // Activity Array
        $data = array(
            'resource_id' => $post_id,
            'type'        => 'post',
            'action'      => 'added',
            'user_id'     => $this->session->userdata('user_id'),
            'message'     => 'A new post was added (' . $data["title"] . ')'
        );

        // Insert Activity
        $this->Activity_model->add($data);

        // Insert Categories into CI_TERMS_TAXONOMY - term_taxonomy_id, term_id, type, body, parent_id, count
        $categories = $this->Terms_model->get_list();
        foreach ($categories as $cat) {
            $data = array(
                'term_taxonomy_id'  => $this->db->insert_id(),
                'term_id'           => $cat->term_id,
                'type'              => 'category',
            );
        }

        $this->Taxonomy_model->add($data);


        // Insert Categories into CI_TERMS_RELATIONSHIP - post_id, term_taxonomy_id, term_order
        $taxonomies = $this->Taxonomy_model->get_list();

        $data = array();

        foreach ($taxonomies as $taxonomy){
            $data[] = array(
                'post_id'           => $post_id,
                'term_taxonomy_id'  => $taxonomy->term_taxonomy_id,
            );
        }

        $this->db->insert_batch('ci_terms_relationship', $data);

        // Set Message
        $this->session->set_flashdata('success', 'Post has been added');

        // Redirect
        redirect('admin/posts');
    }
}

在大多数代码中一切都运行良好,我的意思是我可以将帖子插入其表中,然后我将类别添加到ci_taxonomy表中并且工作正常。当我尝试将当前id添加到post_id时出现问题在CI_relationship表中。

现在它只让我犯这个错误:

  

重复输入' 0-1'关键' PRIMARY'

     

INSERT INTO' ci_terms_relationship' (' post_id',' term_taxonomy_id')   VALUES(0,' 1'),(0,' 2'),(0,' 3'),(0,' 4') ;),(0,' 5'),(0,' 6'),(0,' 7'),   (0,' 8'),(0,' 9')

并且正如它所说,这是因为我没有将post_id列设置为自动增量但是如果我确实更改了它,则无法传递刚刚添加到数据库的帖子的当前id ......任何人都知道如何解决这个问题,或任何提示?

我希望我能够清楚地了解这一点。

提前致谢。

0 个答案:

没有答案