Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

...
  • 12 commits
  • 4 files changed
  • 0 commit comments
  • 4 contributors
27 classes/model.php
View
@@ -592,10 +592,27 @@ public function _update_original($original = null)
/**
* Update the original relations for this object
*/
- public function _update_original_relations()
+ public function _update_original_relations($relations = null)
{
- $this->_original_relations = array();
- foreach ($this->_data_relations as $rel => $data)
+ if (is_null($relations))
+ {
+ $this->_original_relations = array();
+ $relations = $this->_data_relations;
+ }
+ else
+ {
+ foreach ($relations as $key => $rel)
+ {
+ // Unload the just fetched relation from the originals
+ unset($this->_original_relations[$rel]);
+
+ // Unset the numeric key and set the data to update by the relation name
+ unset($relations[$key]);
+ $relations[$rel] = $this->_data_relations[$rel];
+ }
+ }
+
+ foreach ($relations as $rel => $data)
{
if (is_array($data))
{
@@ -661,7 +678,7 @@ public function & __get($property)
if ( ! array_key_exists($property, $this->_data_relations))
{
$this->_data_relations[$property] = $rel->get($this);
- $this->_update_original_relations();
+ $this->_update_original_relations(array($property));
}
return $this->_data_relations[$property];
}
@@ -1100,7 +1117,7 @@ public function __clone()
*/
public static function set_form_fields($form, $instance = null)
{
- Observer_Validation::set_fields(get_called_class(), $form);
+ Observer_Validation::set_fields($instance instanceof static ? $instance : get_called_class(), $form);
$instance and $form->repopulate($instance);
}
5 classes/observer/updatedat.php
View
@@ -19,7 +19,10 @@ class Observer_UpdatedAt extends Observer {
public function before_save(Model $obj)
{
- $obj->{static::$property} = static::$mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
+ if ($obj->is_new() or $obj->is_changed())
+ {
+ $obj->{static::$property} = static::$mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
+ }
}
}
34 classes/observer/validation.php
View
@@ -25,10 +25,11 @@ class Observer_Validation extends Observer {
* @param Fieldset|null
* @return Fieldset
*/
- public static function set_fields($class, $fieldset = null)
+ public static function set_fields($obj, $fieldset = null)
{
- $properties = $class::properties();
+ static $_generated = array();
+ $class = is_object($obj) ? get_class($obj) : $obj;
if (is_null($fieldset))
{
$fieldset = \Fieldset::instance($class);
@@ -38,6 +39,16 @@ public static function set_fields($class, $fieldset = null)
}
}
+ ! array_key_exists($class, $_generated) and $_generated[$class] = array();
+ if (in_array($fieldset, $_generated[$class], true))
+ {
+ return $fieldset;
+ }
+ $_generated[$class][] = $fieldset;
+
+ $fieldset->validation()->add_callable($obj);
+
+ $properties = is_object($obj) ? $obj->properties() : $class::properties();
foreach ($properties as $p => $settings)
{
$field = $fieldset->add($p, ! empty($settings['label']) ? $settings['label'] : $p);
@@ -74,17 +85,28 @@ public static function set_fields($class, $fieldset = null)
*/
public function before_save(Model $obj)
{
- $val = static::set_fields(get_class($obj))->validation();
+ return $this->validate($obj);
+ }
+
+ /**
+ * Validate the model
+ *
+ * @param Model
+ * @throws ValidationFailed
+ */
+ public function validate(Model $obj)
+ {
+ $val = static::set_fields($obj)->validation();
$input = array();
- foreach ($obj as $k => $v)
+ foreach (array_keys($obj->properties()) as $p)
{
- ! in_array($k, $obj->primary_key()) and $input[$k] = $v;
+ ! in_array($p, $obj->primary_key()) and $input[$p] = $obj->{$p};
}
if ($val->run($input) === false)
{
- throw new ValidationFailed();
+ throw new ValidationFailed($val->show_errors());
}
else
{
31 classes/query.php
View
@@ -241,7 +241,6 @@ public function or_where()
*
* @param array
* @param string
- * @todo adding the table alias needs to work better, this will cause problems with WHERE IN
*/
public function _where($condition, $type = 'and_where')
{
@@ -360,7 +359,7 @@ public function order_by($property, $direction = 'ASC')
$property = $this->alias.'.'.$property;
}
- $this->order_by[$property] = $direction;
+ $this->order_by[] = array($property, $direction);
return $this;
}
@@ -479,12 +478,12 @@ public function build_query($query, $columns = array(), $type = 'select')
$order_by = $this->order_by;
if ( ! empty($order_by))
{
- foreach ($order_by as $property => $direction)
+ foreach ($order_by as $key => $ob)
{
- if (strpos($property, $this->alias.'.') === 0)
+ if (strpos($ob[0], $this->alias.'.') === 0)
{
- $query->order_by($type == 'select' ? $property : substr($property, strlen($this->alias.'.')), $direction);
- unset($order_by[$property]);
+ $query->order_by($type == 'select' ? $ob[0] : substr($ob[0], strlen($this->alias.'.')), $ob[1]);
+ unset($order_by[$key]);
}
}
}
@@ -503,7 +502,7 @@ public function build_query($query, $columns = array(), $type = 'select')
{
list($method, $conditional) = $w;
- if (empty($conditional) or $open_nests > 0)
+ if ($type == 'select' and (empty($conditional) or $open_nests > 0))
{
strpos($method, '_open') and $open_nests++;
strpos($method, '_close') and $open_nests--;
@@ -606,11 +605,11 @@ public function build_query($query, $columns = array(), $type = 'select')
{
if (is_int($k_ob))
{
- $order_by[$v_ob] = 'ASC';
+ $order_by[] = array($v_ob, 'ASC');
}
else
{
- $order_by[$k_ob] = $v_ob;
+ $order_by[] = array($k_ob, $v_ob);
}
}
}
@@ -637,17 +636,17 @@ public function build_query($query, $columns = array(), $type = 'select')
// Get the order
if ( ! empty($order_by))
{
- foreach ($order_by as $column => $direction)
+ foreach ($order_by as $ob)
{
// try to rewrite conditions on the relations to their table alias
- $dotpos = strrpos($column, '.');
- $relation = substr($column, 0, $dotpos);
+ $dotpos = strrpos($ob[0], '.');
+ $relation = substr($ob[0], 0, $dotpos);
if ($dotpos > 0 and array_key_exists($relation, $models))
{
- $column = $models[$relation]['table'][1].substr($column, $dotpos);
+ $column = $models[$relation]['table'][1].substr($ob[0], $dotpos);
}
- $query->order_by($column, $direction);
+ $query->order_by($column, $ob[1]);
}
}
@@ -773,7 +772,7 @@ public function hydrate(&$row, $models, &$result, $model = null, $select = null,
// attach the retrieved relations to the object and update its original DB values
$obj->_relate($rel_objs);
- $obj->_update_original();
+ $obj->_update_original_relations();
return $obj;
}
@@ -1068,4 +1067,4 @@ public function delete()
}
}
-/* End of file query.php */
+/* End of file query.php */

No commit comments for this range

Something went wrong with that request. Please try again.