troubleshooting.rst 887 B

123456789101112131415161718192021222324252627282930
  1. Troubleshooting
  2. ===============
  3. The toString method
  4. -------------------
  5. Sometimes the bundle needs to display your model objects, in order to do it, objects are converted to string by using the `__toString`_ magic method.
  6. Take care to never return anything else than a string in this method.
  7. For example, if your method looks like that :
  8. .. code-block:: php
  9. public function __toString()
  10. {
  11. return $this->getTitle();
  12. }
  13. You can't be sure your object will *always* have a title when the bundle will want to convert it to a string.
  14. So in order to avoid any fatal error, you must return an empty string (or anything you prefer) for when the title is missing, like this :
  15. .. code-block:: php
  16. public function __toString()
  17. {
  18. return $this->getTitle() ?: '';
  19. }
  20. .. _`__toString`: http://www.php.net/manual/en/language.oop5.magic.php#object.tostring