HosipLan

Untitled

Aug 30th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace Todo;
  3. use Nette;
  4.  
  5. /**
  6. * Provádí operace nad databázovou tabulkou.
  7. */
  8. abstract class Repository extends Nette\Object
  9. {
  10. /** @var Nette\Database\SelectionFactory */
  11. protected $selectionFactory;
  12.  
  13. public function __construct(\Nette\Database\SelectionFactory $sf)
  14. {
  15. $this->selectionFactory = $sf;
  16. }
  17.  
  18. /**
  19. * Vrací objekt reprezentující databázovou tabulku.
  20. * @return Nette\Database\Table\Selection
  21. */
  22. protected function getTable()
  23. {
  24. // název tabulky odvodíme z názvu třídy
  25. preg_match('#(\w+)Repository$#', get_class($this), $m);
  26. return $this->selectionFactory->table(lcfirst($m[1]));
  27. }
  28.  
  29. /**
  30. * Vrací všechny řádky z tabulky.
  31. * @return Nette\Database\Table\Selection
  32. */
  33. public function findAll()
  34. {
  35. return $this->getTable();
  36. }
  37.  
  38. /**
  39. * Vrací řádky podle filtru, např. array('name' => 'John').
  40. * @return Nette\Database\Table\Selection
  41. */
  42. public function findBy(array $by)
  43. {
  44. return $this->getTable()->where($by);
  45. }
  46.  
  47.  
  48. public function findById($id)
  49. {
  50. return $this->findAll()->get($id);
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment