1: <?php
2: namespace Budovy;
3:
4: use Nette;
5:
6: abstract class Repository extends Nette\Object {
7: /** @var Nette\Database\Connection */
8: protected $connection;
9:
10: /** @var Nette\Database\Table\Selection */
11: protected $table;
12:
13: public function __construct(Nette\Database\Table\Selection $table, Nette\Database\Context $db) {
14: $this->table = $table;
15: $this->connection = $db;
16: }
17:
18: /**
19: * Vrací objekt reprezentující databázovou tabulku.
20: *
21: * @return Nette\Database\Table\Selection
22: */
23: protected function getTable() {
24: return clone $this->table;
25: }
26:
27: /**
28: * Vrací řádky podle filtru, např. array('name' => 'John').
29: *
30: * @return Nette\Database\Table\Selection
31: */
32: public function findBy(array $by) {
33: return $this->getTable()->where($by);
34: }
35:
36: /**
37: * Vrací řádek podle primárního klíče.
38: *
39: * @return Nette\Database\Table\ActiveRow
40: */
41:
42: public function findById($id) {
43: return $this->getTable()->get($id);
44: }
45:
46: /**
47: * Smaže řádek podle primárního klíče.
48: *
49: * @return int
50: */
51:
52: public function deleteById($id) {
53: return $this->getTable()->get($id)->delete();
54: }
55:
56: /**
57: * Update řádku podle primárního klíče.
58: *
59: * @return int
60: */
61:
62: public function updateById($id, $data) {
63: return $this->getTable()->wherePrimary($id)->update($data);
64: }
65:
66: /**
67: * Vloží řádek.
68: *
69: * @return Nette\Database\Table\ActiveRow
70: */
71:
72: public function insertData($data) {
73: return $this->getTable()->insert($data);
74: }
75: }