troubleshooting.rst 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Troubleshooting
  2. ===============
  3. The toString method
  4. -------------------
  5. Sometimes the bundle needs to display your model objects, in order to do it,
  6. objects are converted to string by using the `__toString`_ magic method.
  7. Take care to never return anything else than a string in this method.
  8. For example, if your method looks like that :
  9. .. code-block:: php
  10. <?php
  11. // src/AppBundle/Entity/Post.php
  12. class Post
  13. {
  14. // ...
  15. public function __toString()
  16. {
  17. return $this->getTitle();
  18. }
  19. // ...
  20. }
  21. You cannot be sure your object will *always* have a title when the bundle will want to convert it to a string.
  22. So in order to avoid any fatal error, you must return an empty string
  23. (or anything you prefer) for when the title is missing, like this :
  24. .. code-block:: php
  25. <?php
  26. // src/AppBundle/Entity/Post.php
  27. class Post
  28. {
  29. // ...
  30. public function __toString()
  31. {
  32. return $this->getTitle() ?: '';
  33. }
  34. // ...
  35. }
  36. .. _`__toString`: http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
  37. Large filters and long URLs problem
  38. -----------------------------------
  39. If you will try to add hundreds of filters to a single admin class, you will get a problem - very long generated filter form URL.
  40. In most cases you will get server response like *Error 400 Bad Request* OR *Error 414 Request-URI Too Long*. According to
  41. `a StackOverflow discussion <http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers>`_
  42. "safe" URL length is just around 2000 characters.
  43. You can fix this issue by adding a simple JQuery piece of code on your edit template :
  44. .. code-block:: javascript
  45. $(function() {
  46. // Add class 'had-value-on-load' to inputs/selects with values.
  47. $(".sonata-filter-form input").add(".sonata-filter-form select").each(function(){
  48. if($(this).val()) {
  49. $(this).addClass('had-value-on-load');
  50. }
  51. });
  52. // REMOVE ALL EMPTY INPUT FROM FILTER FORM (except inputs, which has class 'had-value-on-load')
  53. $(".sonata-filter-form").submit(function() {
  54. $(".sonata-filter-form input").add(".sonata-filter-form select").each(function(){
  55. if(!$(this).val() && !$(this).hasClass('had-value-on-load')) {
  56. $(this).remove()
  57. };
  58. });
  59. });
  60. });