tableName = '{@name}'; $this->modelName = '\{@itemNamespace}\Model\{@table.php_name}'; $this->primaryKey = '{@table.primary_key.column}'; } {if table.primary_key} /** * @param $value * @param string $useConnection Connection type to use. * @throws StoreException * @return {@table.php_name} */ public function getByPrimaryKey($value, $useConnection = 'read') { return $this->getBy{@table.primary_key.php_name}($value, $useConnection); } {/if} {ifnot table.primary_key} /** * @param $value * @param $useConnection * @deprecated * @throws StoreException */ public function getByPrimaryKey($value, $useConnection = 'read') { throw new StoreException('getByPrimaryKey is not implemented for this store, as the table has no primary key.'); } {/ifnot} {loop table.columns} {if item.unique_indexed} /** * @param $value * @param string $useConnection Connection type to use. * @throws StoreException * @return {@parent.table.php_name} */ public function getBy{@item.php_name}($value, $useConnection = 'read') { if (is_null($value)) { throw new StoreException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } {if table.primary_key.php_name == item.php_name} // This is the primary key, so try and get from cache: $cacheResult = $this->getFromCache($value); if (!empty($cacheResult)) { return $cacheResult; } {/if} $query = new Query($this->getNamespace('{@table.php_name}').'\Model\{@table.php_name}', $useConnection); $query->select('*')->from('{@parent.name}')->limit(1); $query->where('`{@item.name}` = :{@item.name}'); $query->bind(':{@item.name}', $value); try { $query->execute(); $result = $query->fetch(); $this->setCache($value, $result); return $result; } catch (PDOException $ex) { throw new StoreException('Could not get {@parent.table.php_name} by {@item.php_name}', 0, $ex); } }{/if} {if item.many_indexed} {if counts} /** * @param $value * @param array $options Offsets, limits, etc. * @param string $useConnection Connection type to use. * @throws StoreException * @return int */ public function getTotalFor{@item.php_name}($value, $options = [], $useConnection = 'read') { if (is_null($value)) { throw new StoreException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } $query = new Query($this->getNamespace('{@table.php_name}').'\Model\{@table.php_name}', $useConnection); $query->from('{@parent.name}')->where('`{@item.name}` = :{@item.name}'); $query->bind(':{@item.name}', $value); $this->handleQueryOptions($query, $options); try { return $query->getCount(); } catch (PDOException $ex) { throw new StoreException('Could not get count of {@parent.table.php_name} by {@item.php_name}', 0, $ex); } } {/if} /** * @param $value * @param array $options Limits, offsets, etc. * @param string $useConnection Connection type to use. * @throws StoreException * @return {@parent.table.php_name}Collection */ public function getBy{@item.php_name}($value, $options = [], $useConnection = 'read') { if (is_null($value)) { throw new StoreException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); } $query = new Query($this->getNamespace('{@table.php_name}').'\Model\{@table.php_name}', $useConnection); $query->from('{@parent.name}')->where('`{@item.name}` = :{@item.name}'); $query->bind(':{@item.name}', $value); $this->handleQueryOptions($query, $options); try { $query->execute(); return new {@table.php_name}Collection($query->fetchAll()); } catch (PDOException $ex) { throw new StoreException('Could not get {@parent.table.php_name} by {@item.php_name}', 0, $ex); } }{/if}{/loop} }