grocery crud 多次插入重复数据

grocery crud insert duplicate data multiple time

您好,我正在将 Grocery CRUD 与 HMVC 结合使用,但我遇到了一些奇怪的问题。

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());
            }
    }
}

而在源代码 table 中,我只有一列 "name",另一列是带有自动递增和主键的 ID。

当我添加或插入数据时,它会在 table 中添加多个数据,有时会出现 3 或 4 次重复数据。

我也在用模板库

这是我第一次遇到这个 Grocery Crud 的奇怪问题,谁能帮我解决这个问题。

尝试这样做,控制器:

    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());
            }
        }
    }

需要额外的功能:

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

查看:

<?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>

我确实遇到了多次插入 grocerycrud 数据的问题,我通过在 js 文件中添加以下代码解决了这个问题。

我使用的是数据表主题,文件是 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,
.......

我们需要在点击提交事件后添加"event.stopImmediatePropagation();"

这将有助于停止重复插入。

显示杂货店时列出重复的行是有原因的: - 如果您在 viewtable 之间有一个关系函数 (set_relation_n_n) 那么您必须确保视图有 unique ID 列(没有重复值)否则你会得到重复的行,解决方案是使用: $crud->set_primary_key('id', 'viewname'); 将此行放在关系函数之前。