【问题标题】:grocery crud insert duplicate data multiple time杂货杂货多次插入重复数据
【发布时间】:2015-11-21 10:47:41
【问题描述】:

您好,我正在将 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());
            }
    }
}

在源表中,我只有一列“名称”,另一列是具有自动增量和主键的 id。

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

我也在使用模板库。

这是我第一次遇到这个杂货克鲁德的奇怪问题,谁能帮我解决这个问题。

【问题讨论】:

  • 如果您使用“视图文件”和“头文件”,请检查您没有在两个文件中复制 php 代码。

标签: php codeigniter grocery-crud


【解决方案1】:

尝试这样做,控制器:

    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>

【讨论】:

  • 同样的问题。
  • 试试我刚刚编辑的新代码。不要忘记编辑我离开 cmets 的位置
  • @user2293790 你有teamviewer吗?
  • 我会回来的,我们会比teamviewer
【解决方案2】:

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

我使用的是数据表主题,其中文件是 杂货店_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();”点击后提交事件。

这将有助于阻止多次重复插入。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多