Jay
Jay

Reputation: 841

grocery crud insert duplicate data multiple time

Hi I am using Grocery CRUD with HMVC and I am facing some strange problem with that.

class Source extends MX_Controller
{

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

    function index(){
            try{
                $crud = new grocery_CRUD();

                $crud->set_theme('datatables');
                $crud->set_table('source');
                $crud->set_subject('Source');

                $output = $crud->render();
                $data['css_files'] = $output->css_files;
                $data['js_files'] = $output->js_files;
                $data['output'] = $output->output;

                $this->template->title('Source')
                    ->set_layout('default')
                    ->build('example', $data );

            }catch(Exception $e){
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
            }
    }
}

And in source table I have only one column "name" and other is id with auto increment and primary key.

When I add or insert data, it add multiple data in table some time 3 or 4 times duplicate data.

I am also using template library.

This is strange issue I am facing first time with this Grocery Crud, can any one help me out to solve this.

Upvotes: 3

Views: 2065

Answers (3)

Mohammed Abdellatif
Mohammed Abdellatif

Reputation: 551

there is a reason to get duplicated rows listed when display the grocery crud: - IF YOU HAVE A RELATION function (set_relation_n_n) between a view and table then you have to be sure that the view have unique ID column (with no Duplicated values) else you will got the duplicated row, the solution is to use: $crud->set_primary_key('id', 'viewname'); put this line before relation function.

Upvotes: 0

user3232286
user3232286

Reputation:

I do have same issue with grocerycrud data get inserted multiple time, and I solve this issue by adding following code to the js file.

I was using datatable theme and in that the file was grocery_crud\themes\datatables\js\datatables-add.js

$('#save-and-go-back-button').click(function(event){
        event.stopImmediatePropagation();
        save_and_close = true;

        $('#crudForm').trigger('submit');
});

$('#crudForm').submit(function(event){
        event.stopImmediatePropagation();
        $(this).ajaxSubmit({
            url: validation_url,
.......

we need to add "event.stopImmediatePropagation();" after the click and submit events.

This will help to stop multiple dulplicate insert.

Upvotes: 1

Ilanus
Ilanus

Reputation: 6928

Try doing this, Controller:

    class Source extends MX_Controller
    {
        function __construct() {
            parent::__construct();
        }

        function index(){
            try{
                $crud = new grocery_CRUD();

                $crud->set_theme('datatables');
                $crud->set_table('source');
                $crud->set_subject('Source');

                //edit the id of source table as it's on your table..
                $crud->set_primary_key('id', 'source');

                //set the columns you want
                $crud->columns(
                    'id',
                    'name'
                );

                //how do you want to display them in the frontend
                $crud->display_as('id', 'id');
                $crud->display_as('name', 'name');

                $table = $crud->render();
                $this->_table_output($table);

            }catch(Exception $e){
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
            }
        }
    }

Additional function needed:

private function _table_output($output = null)
{
    //load reservations table
    $this->load->view('yourviewfile', $output);
}

View:

<?php
foreach($output['css_files'] as $file): ?>
    <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<?php foreach($output['js_files'] as $file): ?>
    <script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>

<div>
    <?php echo $output['output']; ?>
</div>

Upvotes: 0

Related Questions