getBy{@table.primary_key.php_name}($value, $useConnection); } {/if} {ifnot table.primary_key} public function getByPrimaryKey($value, $useConnection = 'read') { throw new \Exception('getByPrimaryKey is not implemented for this store, as the table has no primary key.'); } {/ifnot} {loop table.columns} {if item.unique_indexed} public function getBy{@item.php_name}($value, $useConnection = 'read') { if (is_null($value)) { throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } $query = 'SELECT * FROM `{@parent.name}` WHERE `{@item.name}` = :{@item.name} LIMIT 1'; $stmt = Database::getConnection($useConnection)->prepare($query); $stmt->bindValue(':{@item.name}', $value); if ($stmt->execute()) { if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { return new {@parent.table.php_name}($data); } } return null; } {/if} {if item.many_indexed} public function getBy{@item.php_name}($value, $limit = 1000, $useConnection = 'read') { if (is_null($value)) { throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } {if counts} $query = 'SELECT COUNT(*) AS cnt FROM `{@parent.name}` WHERE `{@item.name}` = :{@item.name}'; $stmt = Database::getConnection($useConnection)->prepare($query); $stmt->bindValue(':{@item.name}', $value); if ($stmt->execute()) { $res = $stmt->fetch(\PDO::FETCH_ASSOC); $count = (int)$res['cnt']; } else { $count = 0; } {/if} $query = 'SELECT * FROM `{@parent.name}` WHERE `{@item.name}` = :{@item.name} LIMIT :limit'; $stmt = Database::getConnection($useConnection)->prepare($query); $stmt->bindValue(':{@item.name}', $value); $stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT); if ($stmt->execute()) { $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); $map = function ($item) { return new {@parent.table.php_name}($item); }; $rtn = array_map($map, $res); {ifnot counts}$count = count($rtn);{/ifnot} return array('items' => $rtn, 'count' => $count); } else { return array('items' => array(), 'count' => 0); } } {/if} {/loop} }