SHOW:
|
|
- or go back to the newest paste.
| 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\Connection */ |
| 10 | + | /** @var Nette\Database\SelectionFactory */ |
| 11 | - | protected $connection; |
| 11 | + | protected $selectionFactory; |
| 12 | ||
| 13 | - | public function __construct(Nette\Database\Connection $db) |
| 13 | + | public function __construct(\Nette\Database\SelectionFactory $sf) |
| 14 | - | {
|
| 14 | + | |
| 15 | - | $this->connection = $db; |
| 15 | + | $this->selectionFactory = $sf; |
| 16 | - | } |
| 16 | + | |
| 17 | ||
| 18 | - | /** |
| 18 | + | /** |
| 19 | - | * Vrací objekt reprezentující databázovou tabulku. |
| 19 | + | * Vrací objekt reprezentující databázovou tabulku. |
| 20 | - | * @return Nette\Database\Table\Selection |
| 20 | + | * @return Nette\Database\Table\Selection |
| 21 | - | */ |
| 21 | + | */ |
| 22 | - | protected function getTable() |
| 22 | + | protected function getTable() |
| 23 | - | {
|
| 23 | + | |
| 24 | - | // název tabulky odvodíme z názvu třídy |
| 24 | + | // název tabulky odvodíme z názvu třídy |
| 25 | - | preg_match('#(\w+)Repository$#', get_class($this), $m);
|
| 25 | + | preg_match('#(\w+)Repository$#', get_class($this), $m);
|
| 26 | - | return $this->connection->table(lcfirst($m[1])); |
| 26 | + | return $this->selectionFactory->table(lcfirst($m[1])); |
| 27 | - | } |
| 27 | + | |
| 28 | ||
| 29 | - | /** |
| 29 | + | /** |
| 30 | - | * Vrací všechny řádky z tabulky. |
| 30 | + | * Vrací všechny řádky z tabulky. |
| 31 | - | * @return Nette\Database\Table\Selection |
| 31 | + | * @return Nette\Database\Table\Selection |
| 32 | - | */ |
| 32 | + | */ |
| 33 | - | public function findAll() |
| 33 | + | public function findAll() |
| 34 | - | {
|
| 34 | + | |
| 35 | - | return $this->getTable(); |
| 35 | + | return $this->getTable(); |
| 36 | - | } |
| 36 | + | |
| 37 | ||
| 38 | - | /** |
| 38 | + | /** |
| 39 | - | * Vrací řádky podle filtru, např. array('name' => 'John').
|
| 39 | + | * Vrací řádky podle filtru, např. array('name' => 'John').
|
| 40 | - | * @return Nette\Database\Table\Selection |
| 40 | + | * @return Nette\Database\Table\Selection |
| 41 | - | */ |
| 41 | + | */ |
| 42 | - | public function findBy(array $by) |
| 42 | + | public function findBy(array $by) |
| 43 | - | {
|
| 43 | + | |
| 44 | - | return $this->getTable()->where($by); |
| 44 | + | return $this->getTable()->where($by); |
| 45 | - | } |
| 45 | + | |
| 46 | ||
| 47 | ||
| 48 | public function findById($id) | |
| 49 | {
| |
| 50 | return $this->findAll()->get($id); | |
| 51 | } | |
| 52 | ||
| 53 | } |