/** * Returns the fieldset containing the fields and rules for the entity. * Subclasses should override this method and initialise the Fieldset with * the entity's field definitions - if not already present in the fieldset. * Providing this as a static method enables us to extract the field definitions of an * entity without having to actually create an instance of the entity. * @param \spf\model\Fieldset $fieldset an existing Fieldset to use. * @return \spf\model\Fieldset */ public static function getFields($fieldset = null) { if (!$fieldset) { $fieldset = new Fieldset(); } else { \spf\assert_instance($fieldset, '\\spf\\model\\Fieldset'); } return $fieldset; }
public function find($filter = null) { if ($filter === null) { $filter = $this->filter(); } else { \spf\assert_instance($filter, '\\spf\\model\\Filter'); } list($sql, $params) = $filter->buildSelectQuery('id', $this->mapper); return $this->fetch($this->db->getCol($sql, $params)); }
public function update($entity) { \spf\assert_instance($entity, $this->entity_class); if ($entity->getErrors()) { throw new Exception("Can't update, {$this->entity_class} has errors: " . var_export($entity->getErrors(), true)); } elseif (!$entity->hasId()) { throw new Exception('Can\'t update, entity has no id'); } list($sql, $params) = $this->buildAssignmentList($entity, $entity->isDirty(), array('id')); ($before = $this->beforeUpdate($entity, $sql, $params)) && (list($sql, $params) = $before); // make sure update wasn't cancelled and there's something to actually do if ($before && $sql) { $sql = strtr("UPDATE `{table}`\nSET {values}\nWHERE `{field}` = :id", array('{table}' => $this->db_table, '{values}' => substr($sql, 0, -2), '{field}' => $this->getDbFieldName('id'))); $this->db->execute($sql, $params + array('id' => $entity->id)); } $this->afterUpdate($entity); $entity->markClean(); return true; }
/** * Inject a profiler object. * * @param \spf\util\Profiler $profiler * @return self */ public function setProfiler($profiler) { $profiler !== null || \spf\assert_instance($profiler, '\\spf\\util\\Profiler'); $this->profiler = $profiler; return $this; }
public function buildQuery($mapper, $prefix = '') { \spf\assert_instance($mapper, '\\spf\\model\\DataMapper'); $query = $this->decode($mapper); if ($query['where']) { $query['where'] = 'WHERE ' . substr($query['where'], 4, -1); } if ($query['orderby']) { $query['orderby'] = 'ORDER BY ' . substr($query['orderby'], 0, -2); } $db_table = $mapper->getDbTable(); $sql = strtr("{prefix}\nFROM `{table}` AS {alias}\n{joins}\n{where}\n{groupby}\n{orderby}\n{limit}", array('{prefix}' => $prefix, '{field}' => $mapper->getDbFieldName('id'), '{table}' => $db_table, '{alias}' => $mapper->getTableAlias($db_table), '{joins}' => $query['joins'], '{where}' => $query['where'], '{groupby}' => $query['groupby'], '{orderby}' => $query['orderby'], '{limit}' => $query['limit'])); $sql = trim(preg_replace("/\n{2,}/", "\n", $sql)); return array($sql, $query['params']); }
/** * Assigns a new item to the specified key. * * @param string $key The key of the item * @param mixed $item The item to set * @return self */ public function set($key, $item) { \spf\assert_instance($item, $this->class); $this->items[$key] = $item; return $this; }