• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[mongoDB+CI] Can't insert & update

#1
[eluser]zacksyah[/eluser]
I Can't insert & update data with mongoDB. is there something wrong with my code? I've tried insert data with mongoDB & without CI [normal PHP] and it's work. This below the code
Code:
<?php

class Blog extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');

        $this->load->model('author_model', 'author');
        $this->load->model('post_model', 'post');
    }

    function index() {
        $data['main'] = 'templates/add-author';
        $this->load->view('index', $data);
    }

    function add_author() {
        $data['main'] = 'templates/add-author';
        $this->load->view('index', $data);
    }

    function save_author() {
        $this->load->library('form_validation');

        if ($this->form_validation->run('author') == FALSE) {
            $this->session->set_flashdata('error_message', validation_errors());
            redirect('blog/add_author');
        } else {
            $this->author->save();
            $this->session->set_flashdata('message', 'Author data has been saved');
            redirect('blog/view_author');
        }
    }

    function view_author($offset='') {
        $this->load->library('pagination');

        $per_page = 10;
        $base_url = base_url() . 'blog/view_author';
        $total = $this->author->count();

        $data['authors'] = $this->author->all_pagination($per_page, $offset);

        $config['per_page'] = $per_page;
        $config['total_rows'] = $total;
        $config['uri_segment'] = 3;
        $config['base_url'] = $base_url;

        $this->pagination->initialize($config);

        $data['create_links'] = $this->pagination->create_links();
        $data['main'] = 'templates/view-author';

        $this->load->view('index', $data);
    }

    function delete_author($_id) {
        $this->author->delete($_id);
        $this->session->set_flashdata('message', 'Author data has been deleted');
        redirect('blog/view_author');
    }

    function detail_author($_id) {
        $this->session->set_userdata('current_url', '' . base_url() . 'blog/detail_author/' . $_id);
        $data['author'] = $this->author->find_by_id($_id);
        $data['main'] = 'templates/update-author';
        $this->load->view('index', $data);
    }

    function update_author() {
        $this->load->library('form_validation');

        if ($this->form_validation->run('author') == FALSE) {
            $this->session->set_flashdata('error_message', validation_errors());
            redirect($this->input->post('current_url'));
        } else {
            $this->author->update($this->input->post('_id'));
            $this->session->set_flashdata('message', 'Author data has been updated');
            redirect('blog/view_author');
        }
    }
Code:
<?php

class Author_Model extends CI_Model {

    function __contruct() {
        parent::__construct();
    }

    function save() {
        $author = array(
            'fullname' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'nickname' => $this->input->post('nickname'),
        );
        $this->mongo->db->authors->insert($author);
    }

    function all() {
        $authors = array();
        $authors = $this->mongo->db->authors->find();
        return $authors;
    }

    function all_dropdown() {
        $authors = array();

        $query = $this->mongo->db->authors->find(array(), array('_id' => 1, 'nickname' => 1));

        foreach ($query as $row) {
            $authors[(string) $row['_id']] = $row['nickname'];
        }

        return $authors;
    }

    function update($_id) {
        $author = array(
            'fullname' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'nickname' => $this->input->post('nickname')
        );
        $criteria = array('_id' => new MongoID($_id));
        $this->mongo->db->authors->update($criteria, array('$set' => $author));
    }

    function find_by_id($_id) {
        $author = array();
        $author = $this->mongo->db->authors->findOne(array('_id' => new MongoID($_id)));
        return $author;
    }

    function delete($_id) {
        $criteria = $this->mongo->db->authors->findOne(array('_id' => new MongoID($_id)));
        $this->mongo->db->authors->remove(array('_id' => $criteria['_id']), array('safe' => true));
    }

    function all_pagination($limit, $offset) {
        $authors = array();
        if ($offset) {
            $authors = $this->mongo->db->authors->find()->limit($limit)->skip($offset)->sort(array('_id' => -1));
        } else {
            $authors = $this->mongo->db->authors->find()->limit($limit)->sort(array('_id' => -1));
        }
        return $authors;
    }

    function count() {
        $total = $this->mongo->db->authors->count();
        return $total;
    }

}
?>
add-author view : http://pastebin.com/GbHMCWPq
please help me, thank's in advance

#2
[eluser]Glyn[/eluser]
This is my working example if this helps.
Code available to download at http://glynrob.com/database/mongodb-in-codeigniter/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.