1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132:
<?php
namespace BaseModule\Repository;
/**
* Abstract repository, all other repositories are inherited from this one
*/
abstract class BaseRepository extends \Nette\Object {
/**
* Connection to database
* @var Nette\Database\Connection
*/
protected $connection;
/**
* Constructor loads connection
*
* @param \Nette\Database\Connection $db
*/
public function __construct(\Nette\Database\Connection $db) {
$this->connection = $db;
$this->connection->databaseReflection = new \BaseModule\DiscoveredViewReflection();
// $this->connection->query('SET datestyle TO Europe');
//$this->connection->query('SET search_path TO rpredict');
}
/**
* Returns object representing database table
*
* @param string $tableName name of database table
* @return \Nette\Database\Table\Selection
*/
public function getTable($tableName = NULL) {
if ($tableName == NULL) {
return $this->connection->table($this->getTableName());
} else {
return $this->connection->table($tableName);
}
}
/**
* Gets table name from class name
*
* @return String
*/
protected function getTableName() {
// gets table name from class name (e.g. UserRepository)
preg_match('#(\w+)Repository$#', get_class($this), $m);
return \Nette\Utils\Strings::lower($m[1]);
}
/**
* Helper function, trims data array for only specified (allowed) keys
* - used typically for safety reasons, should be used before saving to DB
* - e.g. $dataToSubmit = trimArray(array('safeAttr1', 'safeAttr2'), $dataFromForm));
*
* @param array $allowedKeys array of strings
* @param array $data data array
* @return array
*/
protected function trimArray($allowedKeys, $data) {
foreach ($data as $key => $value) {
if (!in_array($key, $allowedKeys)) {
unset($data[$key]);
}
}
return $data;
}
/**
* Return all rows from table
*
* @return Nette\Database\Table\Selection
*/
public function findAll() {
return $this->getTable();
}
/**
* Return rows according to filter, e.g. array('name' => 'John')
*
* @param array $by criteria, by which data are filtered
* @return \Nette\Database\Table\Selection
*/
public function findBy(array $by) {
return $this->getTable()->where($by);
}
/**
* Returns one row
*
* @param int $id
* @return \Nette\Database\Table\Selection
*/
public function findById($id) {
return $this->getTable()->get($id);
}
/**
* Insert new row
*
* @param array $data
* @return \Nette\Database\Table\ActiveRow
*/
public function insert(array $data) {
return $this->getTable()->insert($data);
}
/**
* Updates specified row
*
* @param int $id
* @param array $data
* @return int number of affected rows
*/
public function update($id, array $data) {
return $this->getTable()->where(array('id' => $id))->update($data);
}
/**
* Returns DB connection
*
* @return \Nette\Database\Connection
*/
public function getConnection() {
return $this->connection;
}
}