From: Cameron Ball Date: Wed, 17 Sep 2014 16:47:33 +0000 (+0800) Subject: More work on simfile repository. X-Git-Url: https://git.cameron1729.xyz/?a=commitdiff_plain;h=0934706c6297f8e7d46aceead43b5d1b469590d8;p=rock.divinelegy.git More work on simfile repository. --- diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index 6401f19..1e49010 100644 --- a/DataAccess/DataMapper/DataMapper.php +++ b/DataAccess/DataMapper/DataMapper.php @@ -26,17 +26,13 @@ class DataMapper implements IDataMapper $this->_maps = include $maps; } - public function find($id, $entity) + public function find($entityName, $queryString) { - return $this->findRange($id, $entity, 1); - } - - public function findRange($id, $entity, $limit) - { - $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id>=%u LIMIT %u', - $this->_maps[$entity]['table'], - $id, - $limit)); + $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE %s', + $this->_maps[$entityName]['table'], + $queryString + )); + $statement->execute(); $rows = $statement->fetchAll(); @@ -44,8 +40,8 @@ class DataMapper implements IDataMapper foreach($rows as $row) { - $className = $this->_maps[$entity]['class']; //the entity to instantiate and return - $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db); + $className = $this->_maps[$entityName]['class']; //the entity to instantiate and return + $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entityName, $row, $this->_db); if(count($constructors) == 0) { @@ -60,6 +56,20 @@ class DataMapper implements IDataMapper } return count($entities) > 1 ? $entities : reset($entities); + + return $this->findRange($id, $entityName, 1); + } + + public function findById($id, $entity) + { + $queryString = sprintf('id=%u', $id); + return $this->find($entity, $queryString); + } + + public function findRange($id, $entity, $limit) + { + $queryString = sprintf('id>=%u LIMIT %u', $id, $limit); + return $this->find($entity, $queryString); } public function save(IDivineEntity $entity) diff --git a/DataAccess/DataMapper/IDataMapper.php b/DataAccess/DataMapper/IDataMapper.php index 987bd82..ee90de0 100644 --- a/DataAccess/DataMapper/IDataMapper.php +++ b/DataAccess/DataMapper/IDataMapper.php @@ -8,10 +8,12 @@ interface IDataMapper { //TODO: Table is the wrong name. We actually give the implementation the entity name and it finds the table from the maps. + //find in table based on criteria in queryString + public function find($entityName, $queryString); //find id in table and return it as an entity - public function find($id, $table); + public function findById($id, $entityName); //find rows with id >= id and stop at limit - public function findRange($id, $table, $limit); + public function findRange($id, $entityName, $limit); //insert/update entity in table public function save(IDivineEntity $entity); //remove entity from table diff --git a/DataAccess/IRepository.php b/DataAccess/IRepository.php new file mode 100644 index 0000000..34765cb --- /dev/null +++ b/DataAccess/IRepository.php @@ -0,0 +1,13 @@ +dataMapper = $dataMapper; } - public function find($id) { - return $this->dataMapper->find($id, 'Simfile'); + public function findById($id) { + return $this->dataMapper->findById($id, 'Simfile'); } public function findRange($id, $limit)