question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] ReorderOperation with more than 200 records

See original GitHub issue

Bug report

What I did

I set up a model that uses ReorderOperation.

What I expected to happen

Reordering and saving stores the information in the database and after refresh of the view, the data is still shown with the new order.

What happened

Order was not persisted into the database. After a refresh, the old order was shown.

What is the cause of the issue

When there are more than 200 records to reorder (not an edge case with a bigger shop with multiple subcategories), the $.ajax request data gets cut inside PHP and PHP receives only the first 200 records. The reason is that data is sent as an array from JS and PHP has a max_input_vars which is by default 1000. Every record in the array (every model item) contains multiple properties ({item_id: "1", parent_id: 5, depth: 0, left: 11 right: 21}), so 200 multiplied by 5.

What I’ve already tried to fix it

There are two possible solutions:

  1. Increase the max_input_vars value. I do not like this because of the possible denial of service concerns and changing the PHP ini is not always easy for all hostings.
  2. Instead of sending an array, send the data as json string and decode it back when received in PHP. This requires two changes in the code:
    1. In vendor/backpack/crud/src/resources/views/crud/reorder.blade.php replace

      $.ajax({
          url: '{{ url(Request::path()) }}',
          type: 'POST',
          data: { tree: arraied },
      })
      

      with

      $.ajax({
          url: '{{ url(Request::path()) }}',
          type: 'POST',
          data: { tree: JSON.stringify(arraied) },
      })
      
    2. In vendor/backpack/crud/src/app/Http/Controllers/Operations/ReorderOperation.php replace

      public function saveReorder()
      {
          $this->crud->hasAccessOrFail('reorder');
      
          $all_entries =\Request::input('tree');
      
          if (count($all_entries)) {
              $count = $this->crud->updateTreeOrder($all_entries);
          } else {
              return false;
          }
      
          return 'success for '.$count.' items';
      }
      

      with

      public function saveReorder()
      {
          $this->crud->hasAccessOrFail('reorder');
      
          $all_entries = json_decode(\Request::input('tree'), true);
      
          if (count($all_entries)) {
              $count = $this->crud->updateTreeOrder($all_entries);
          } else {
              return false;
          }
      
          return 'success for '.$count.' items';
      }
      

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

PHP 8.0.5 (cli) (built: May 3 2021 11:30:57) ( NTS ) Copyright © The PHP Group Zend Engine v4.0.5, Copyright © Zend Technologies with Zend OPcache v8.0.5, Copyright ©, by Zend Technologies

LARAVEL VERSION:

v8.40.0@a654897ad7f97aea9d7ef292803939798c4a02a4

BACKPACK VERSION:

4.1.43@973e8ab33d42f02272f65e9688ea250fc9502d3d

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
o15a3d4l11s2commented, May 24, 2021

I agree this is a breaking change, @tabacitu. I can submit a PR in the next 2 days.

1reaction
Innovicontcommented, Feb 7, 2022

Thanks! It’s work, you save my time…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common error messages in Data Loader - Salesforce Help
Here's a list of common Data Loader errors you'll receive as you use Data Loader to update or insert records. Resolution. Error: Row...
Read more >
BatchWriteItem - Amazon DynamoDB - AWS Documentation
The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can transmit up to 16MB...
Read more >
12 Salesforce Apex Best Practices
This is mainly true for triggers, where up to 200 records could be run ... SOQL and DML are some of the most...
Read more >
Rule 60. Relief from a Judgment or Order - Law.Cornell.Edu
The court may correct a clerical mistake or a mistake arising from oversight or omission whenever one is found in a judgment, order,...
Read more >
how can I Update top 100 records in sql server - Stack Overflow
I admit that, /usually/, when you're using TOP odds are you should be using it with ORDER BY because what you're interested in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found