Skip to content
This repository
  • 12 commits
  • 4 files changed
  • 0 comments
  • 4 contributors
27  classes/model.php
@@ -592,10 +592,27 @@ public function _update_original($original = null)
592 592
 	/**
593 593
 	 * Update the original relations for this object
594 594
 	 */
595  
-	public function _update_original_relations()
  595
+	public function _update_original_relations($relations = null)
596 596
 	{
597  
-		$this->_original_relations = array();
598  
-		foreach ($this->_data_relations as $rel => $data)
  597
+		if (is_null($relations))
  598
+		{
  599
+			$this->_original_relations = array();
  600
+			$relations = $this->_data_relations;
  601
+		}
  602
+		else
  603
+		{
  604
+			foreach ($relations as $key => $rel)
  605
+			{
  606
+				// Unload the just fetched relation from the originals
  607
+				unset($this->_original_relations[$rel]);
  608
+
  609
+				// Unset the numeric key and set the data to update by the relation name
  610
+				unset($relations[$key]);
  611
+				$relations[$rel] = $this->_data_relations[$rel];
  612
+			}
  613
+		}
  614
+
  615
+		foreach ($relations as $rel => $data)
599 616
 		{
600 617
 			if (is_array($data))
601 618
 			{
@@ -661,7 +678,7 @@ public function & __get($property)
661 678
 			if ( ! array_key_exists($property, $this->_data_relations))
662 679
 			{
663 680
 				$this->_data_relations[$property] = $rel->get($this);
664  
-				$this->_update_original_relations();
  681
+				$this->_update_original_relations(array($property));
665 682
 			}
666 683
 			return $this->_data_relations[$property];
667 684
 		}
@@ -1100,7 +1117,7 @@ public function __clone()
1100 1117
 	 */
1101 1118
 	public static function set_form_fields($form, $instance = null)
1102 1119
 	{
1103  
-		Observer_Validation::set_fields(get_called_class(), $form);
  1120
+		Observer_Validation::set_fields($instance instanceof static ? $instance : get_called_class(), $form);
1104 1121
 		$instance and $form->repopulate($instance);
1105 1122
 	}
1106 1123
 
5  classes/observer/updatedat.php
@@ -19,7 +19,10 @@ class Observer_UpdatedAt extends Observer {
19 19
 
20 20
 	public function before_save(Model $obj)
21 21
 	{
22  
-		$obj->{static::$property} = static::$mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
  22
+		if ($obj->is_new() or $obj->is_changed())
  23
+		{
  24
+			$obj->{static::$property} = static::$mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
  25
+		}
23 26
 	}
24 27
 }
25 28
 
34  classes/observer/validation.php
@@ -25,10 +25,11 @@ class Observer_Validation extends Observer {
25 25
 	 * @param   Fieldset|null
26 26
 	 * @return  Fieldset
27 27
 	 */
28  
-	public static function set_fields($class, $fieldset = null)
  28
+	public static function set_fields($obj, $fieldset = null)
29 29
 	{
30  
-		$properties = $class::properties();
  30
+		static $_generated = array();
31 31
 
  32
+		$class = is_object($obj) ? get_class($obj) : $obj;
32 33
 		if (is_null($fieldset))
33 34
 		{
34 35
 			$fieldset = \Fieldset::instance($class);
@@ -38,6 +39,16 @@ public static function set_fields($class, $fieldset = null)
38 39
 			}
39 40
 		}
40 41
 
  42
+		! array_key_exists($class, $_generated) and $_generated[$class] = array();
  43
+		if (in_array($fieldset, $_generated[$class], true))
  44
+		{
  45
+			return $fieldset;
  46
+		}
  47
+		$_generated[$class][] = $fieldset;
  48
+
  49
+		$fieldset->validation()->add_callable($obj);
  50
+
  51
+		$properties = is_object($obj) ? $obj->properties() : $class::properties();
41 52
 		foreach ($properties as $p => $settings)
42 53
 		{
43 54
 			$field = $fieldset->add($p, ! empty($settings['label']) ? $settings['label'] : $p);
@@ -74,17 +85,28 @@ public static function set_fields($class, $fieldset = null)
74 85
 	 */
75 86
 	public function before_save(Model $obj)
76 87
 	{
77  
-		$val = static::set_fields(get_class($obj))->validation();
  88
+		return $this->validate($obj);
  89
+	}
  90
+
  91
+	/**
  92
+	 * Validate the model
  93
+	 *
  94
+	 * @param   Model
  95
+	 * @throws  ValidationFailed
  96
+	 */
  97
+	public function validate(Model $obj)
  98
+	{
  99
+		$val = static::set_fields($obj)->validation();
78 100
 
79 101
 		$input = array();
80  
-		foreach ($obj as $k => $v)
  102
+		foreach (array_keys($obj->properties()) as $p)
81 103
 		{
82  
-			! in_array($k, $obj->primary_key()) and $input[$k] = $v;
  104
+			! in_array($p, $obj->primary_key()) and $input[$p] = $obj->{$p};
83 105
 		}
84 106
 
85 107
 		if ($val->run($input) === false)
86 108
 		{
87  
-			throw new ValidationFailed();
  109
+			throw new ValidationFailed($val->show_errors());
88 110
 		}
89 111
 		else
90 112
 		{
31  classes/query.php
@@ -241,7 +241,6 @@ public function or_where()
241 241
 	 *
242 242
 	 * @param  array
243 243
 	 * @param  string
244  
-	 * @todo   adding the table alias needs to work better, this will cause problems with WHERE IN
245 244
 	 */
246 245
 	public function _where($condition, $type = 'and_where')
247 246
 	{
@@ -360,7 +359,7 @@ public function order_by($property, $direction = 'ASC')
360 359
 			$property = $this->alias.'.'.$property;
361 360
 		}
362 361
 
363  
-		$this->order_by[$property] = $direction;
  362
+		$this->order_by[] = array($property, $direction);
364 363
 
365 364
 		return $this;
366 365
 	}
@@ -479,12 +478,12 @@ public function build_query($query, $columns = array(), $type = 'select')
479 478
 		$order_by = $this->order_by;
480 479
 		if ( ! empty($order_by))
481 480
 		{
482  
-			foreach ($order_by as $property => $direction)
  481
+			foreach ($order_by as $key => $ob)
483 482
 			{
484  
-				if (strpos($property, $this->alias.'.') === 0)
  483
+				if (strpos($ob[0], $this->alias.'.') === 0)
485 484
 				{
486  
-					$query->order_by($type == 'select' ? $property : substr($property, strlen($this->alias.'.')), $direction);
487  
-					unset($order_by[$property]);
  485
+					$query->order_by($type == 'select' ? $ob[0] : substr($ob[0], strlen($this->alias.'.')), $ob[1]);
  486
+					unset($order_by[$key]);
488 487
 				}
489 488
 			}
490 489
 		}
@@ -503,7 +502,7 @@ public function build_query($query, $columns = array(), $type = 'select')
503 502
 			{
504 503
 				list($method, $conditional) = $w;
505 504
 
506  
-				if (empty($conditional) or $open_nests > 0)
  505
+				if ($type == 'select' and (empty($conditional) or $open_nests > 0))
507 506
 				{
508 507
 					strpos($method, '_open') and $open_nests++;
509 508
 					strpos($method, '_close') and $open_nests--;
@@ -606,11 +605,11 @@ public function build_query($query, $columns = array(), $type = 'select')
606 605
 				{
607 606
 					if (is_int($k_ob))
608 607
 					{
609  
-						$order_by[$v_ob] = 'ASC';
  608
+						$order_by[] = array($v_ob, 'ASC');
610 609
 					}
611 610
 					else
612 611
 					{
613  
-						$order_by[$k_ob] = $v_ob;
  612
+						$order_by[] = array($k_ob, $v_ob);
614 613
 					}
615 614
 				}
616 615
 			}
@@ -637,17 +636,17 @@ public function build_query($query, $columns = array(), $type = 'select')
637 636
 		// Get the order
638 637
 		if ( ! empty($order_by))
639 638
 		{
640  
-			foreach ($order_by as $column => $direction)
  639
+			foreach ($order_by as $ob)
641 640
 			{
642 641
 				// try to rewrite conditions on the relations to their table alias
643  
-				$dotpos = strrpos($column, '.');
644  
-				$relation = substr($column, 0, $dotpos);
  642
+				$dotpos = strrpos($ob[0], '.');
  643
+				$relation = substr($ob[0], 0, $dotpos);
645 644
 				if ($dotpos > 0 and array_key_exists($relation, $models))
646 645
 				{
647  
-					$column = $models[$relation]['table'][1].substr($column, $dotpos);
  646
+					$column = $models[$relation]['table'][1].substr($ob[0], $dotpos);
648 647
 				}
649 648
 
650  
-				$query->order_by($column, $direction);
  649
+				$query->order_by($column, $ob[1]);
651 650
 			}
652 651
 		}
653 652
 
@@ -773,7 +772,7 @@ public function hydrate(&$row, $models, &$result, $model = null, $select = null,
773 772
 
774 773
 		// attach the retrieved relations to the object and update its original DB values
775 774
 		$obj->_relate($rel_objs);
776  
-		$obj->_update_original();
  775
+		$obj->_update_original_relations();
777 776
 
778 777
 		return $obj;
779 778
 	}
@@ -1068,4 +1067,4 @@ public function delete()
1068 1067
 	}
1069 1068
 }
1070 1069
 
1071  
-/* End of file query.php */
  1070
+/* End of file query.php */

No commit comments for this range

Something went wrong with that request. Please try again.