/
orm.php
1360 lines (1172 loc) · 30.9 KB
/
orm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php defined('SYSPATH') or die('No direct script access.');
/**
* [Object Relational Mapping][ref-orm] (ORM) is a method of abstracting database
* access to standard PHP calls. All table rows are represented as model objects,
* with object properties representing row data. ORM in Kohana generally follows
* the [Active Record][ref-act] pattern.
*
* [ref-orm]: http://wikipedia.org/wiki/Object-relational_mapping
* [ref-act]: http://wikipedia.org/wiki/Active_record
*
* $Id: ORM.php 4427 2009-06-19 23:31:36Z jheathco $
*
* @package ORM
* @author Kohana Team
* @copyright (c) 2007-2009 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class Kohana_ORM {
// Stores column information for ORM models
protected static $_column_cache = array();
// Callable database methods
protected static $_db_methods = array
(
'where', 'and_where', 'or_where', 'where_open', 'and_where_open', 'or_where_open', 'where_close',
'and_where_close', 'or_where_close', 'distinct', 'select', 'from', 'join', 'on', 'group_by',
'having', 'and_having', 'or_having', 'having_open', 'and_having_open', 'or_having_open',
'having_close', 'and_having_close', 'or_having_close', 'order_by', 'limit', 'offset', 'cached'
);
// Members that have access methods
protected static $_properties = array
(
'object_name', 'object_plural', 'loaded', 'saved', // Object
'primary_key', 'primary_val', 'table_name', 'table_columns', // Table
'has_one', 'belongs_to', 'has_many', 'has_many_through', 'load_with', // Relationships
'validate' // Validation
);
/**
* Creates and returns a new model.
*
* @chainable
* @param string model name
* @param mixed parameter for find()
* @return ORM
*/
public static function factory($model, $id = NULL)
{
// Set class name
$model = 'Model_'.ucfirst($model);
return new $model($id);
}
// Current relationships
protected $_has_one = array();
protected $_belongs_to = array();
protected $_has_many = array();
// Relationships that should always be joined
protected $_load_with = array();
// Validation members
protected $_validate = NULL;
protected $_rules = array();
protected $_callbacks = array();
protected $_filters = array();
protected $_labels = array();
// Current object
protected $_object = array();
protected $_changed = array();
protected $_related = array();
protected $_loaded = FALSE;
protected $_saved = FALSE;
protected $_sorting;
// Foreign key suffix
protected $_foreign_key_suffix = '_id';
// Model table information
protected $_object_name;
protected $_object_plural;
protected $_table_name;
protected $_table_columns;
protected $_ignored_columns = array();
// Auto-update columns for creation and updates
protected $_updated_column = NULL;
protected $_created_column = NULL;
// Table primary key, primary value
protected $_primary_key = 'id';
protected $_primary_val = 'name';
// Primary key value
protected $_primary_key_value;
// Model configuration
protected $_table_names_plural = TRUE;
protected $_reload_on_wakeup = TRUE;
// Database configuration
protected $_db = 'default';
protected $_db_applied = array();
protected $_db_pending = array();
protected $_db_reset = TRUE;
protected $_db_builder;
// With calls already applied
protected $_with_applied = array();
// Data to be loaded into the model from a database call cast
protected $_cast_data = array();
/**
* Constructs a new model and loads a record if given
*
* @param mixed parameter for find or object to load
* @return void
*/
public function __construct($id = NULL)
{
$this->_initialize();
if ($id !== NULL)
{
if (is_array($id))
{
foreach ($id as $column => $value)
{
// Passing an array of column => values
$this->where($column, '=', $value);
}
$this->find();
}
else
{
// Passing the primary key
$this->find($id);
}
}
elseif ( ! empty($this->_cast_data))
{
// Load preloaded data from a database call cast
$this->_load_values($this->_cast_data);
$this->_cast_data = array();
}
}
/**
* Prepares the model database connection, determines the table name,
* and loads column information.
*
* @return void
*/
protected function _initialize()
{
// Set the object name and plural name
$this->_object_name = strtolower(substr(get_class($this), 6));
$this->_object_plural = Inflector::plural($this->_object_name);
if ( ! is_object($this->_db))
{
// Get database instance
$this->_db = Database::instance($this->_db);
}
if (empty($this->_table_name))
{
// Table name is the same as the object name
$this->_table_name = $this->_object_name;
if ($this->_table_names_plural === TRUE)
{
// Make the table name plural
$this->_table_name = Inflector::plural($this->_table_name);
}
}
if ( ! isset($this->_sorting))
{
// Default sorting
$this->_sorting = array($this->_primary_key => 'ASC');
}
if ( ! empty($this->_ignored_columns))
{
// Optimize for performance
$this->_ignored_columns = array_combine($this->_ignored_columns, $this->_ignored_columns);
}
foreach ($this->_belongs_to as $alias => $details)
{
$defaults['model'] = $alias;
$defaults['foreign_key'] = $alias.$this->_foreign_key_suffix;
$this->_belongs_to[$alias] = array_merge($defaults, $details);
}
foreach ($this->_has_one as $alias => $details)
{
$defaults['model'] = $alias;
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
$this->_has_one[$alias] = array_merge($defaults, $details);
}
foreach ($this->_has_many as $alias => $details)
{
$defaults['model'] = Inflector::singular($alias);
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
$defaults['through'] = NULL;
$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
$this->_has_many[$alias] = array_merge($defaults, $details);
}
// Load column information
$this->reload_columns();
// Clear initial model state
$this->clear();
}
/**
* Reload column definitions.
*
* @chainable
* @param boolean force reloading
* @return ORM
*/
public function reload_columns($force = FALSE)
{
if ($force === TRUE OR empty($this->_table_columns))
{
if (isset(ORM::$_column_cache[$this->_object_name]))
{
// Use cached column information
$this->_table_columns = ORM::$_column_cache[$this->_object_name];
}
else
{
// Grab column information from database
$this->_table_columns = $this->list_columns(TRUE);
// Load column cache
ORM::$_column_cache[$this->_object_name] = $this->_table_columns;
}
}
return $this;
}
/**
* Unloads the current object and clears the status.
*
* @chainable
* @return ORM
*/
public function clear()
{
// Create an array with all the columns set to NULL
$values = array_combine(array_keys($this->_table_columns), array_fill(0, count($this->_table_columns), NULL));
// Replace the object and reset the object status
$this->_object = $this->_changed = $this->_related = array();
// Replace the current object with an empty one
$this->_load_values($values);
// Reset primary key
$this->_primary_key_value = NULL;
$this->reset();
return $this;
}
/**
* Reloads the current object from the database.
*
* @chainable
* @return ORM
*/
public function reload()
{
$primary_key = $this->pk();
// Replace the object and reset the object status
$this->_object = $this->_changed = $this->_related = array();
return $this->find($primary_key);
}
/**
* Checks if object data is set.
*
* @param string column name
* @return boolean
*/
public function __isset($column)
{
return
(
isset($this->_object[$column]) OR
isset($this->_related[$column]) OR
isset($this->_has_one[$column]) OR
isset($this->_belongs_to[$column]) OR
isset($this->_has_many[$column])
);
}
/**
* Unsets object data.
*
* @param string column name
* @return void
*/
public function __unset($column)
{
unset($this->_object[$column], $this->_changed[$column], $this->_related[$column]);
}
/**
* Displays the primary key of a model when it is converted to a string.
*
* @return string
*/
public function __toString()
{
return (string) $this->pk();
}
/**
* Allows serialization of only the object data and state, to prevent
* "stale" objects being unserialized, which also requires less memory.
*
* @return array
*/
public function __sleep()
{
// Store only information about the object
return array('_object_name', '_object', '_changed', '_loaded', '_saved', '_sorting');
}
/**
* Prepares the database connection and reloads the object.
*
* @return void
*/
public function __wakeup()
{
// Initialize model
$this->_initialize();
if ($this->_reload_on_wakeup === TRUE)
{
// Reload the object
$this->reload();
}
}
/**
* Handles pass-through to database methods. Calls to query methods
* (query, get, insert, update) are not allowed. Query builder methods
* are chainable.
*
* @param string method name
* @param array method arguments
* @return mixed
*/
public function __call($method, array $args)
{
if (in_array($method, ORM::$_properties))
{
if ($method === 'validate')
{
if ( ! isset($this->_validate))
{
// Initialize the validation object
$this->_validate();
}
}
// Return the property
return $this->{'_'.$method};
}
elseif (in_array($method, ORM::$_db_methods))
{
// Add pending database call which is executed after query type is determined
$this->_db_pending[] = array('name' => $method, 'args' => $args);
return $this;
}
else
{
throw new Kohana_Exception('Invalid method :method called in :class',
array(':method' => $method, ':class' => get_class($this)));
}
}
/**
* Handles retrieval of all model values, relationships, and metadata.
*
* @param string column name
* @return mixed
*/
public function __get($column)
{
if (array_key_exists($column, $this->_object))
{
return $this->_object[$column];
}
elseif (isset($this->_related[$column]) AND $this->_related[$column]->_loaded)
{
// Return related model that has already been loaded
return $this->_related[$column];
}
elseif (isset($this->_belongs_to[$column]))
{
$model = $this->_related($column);
// Use this model's column and foreign model's primary key
$col = $model->_table_name.'.'.$model->_primary_key;
$val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
$model->where($col, '=', $val)->find();
return $this->_related[$column] = $model;
}
elseif (isset($this->_has_one[$column]))
{
$model = $this->_related($column);
// Use this model's primary key value and foreign model's column
$col = $model->_table_name.'.'.$this->_has_one[$column]['foreign_key'];
$val = $this->pk();
$model->where($col, '=', $val)->find();
return $this->_related[$column] = $model;
}
elseif (isset($this->_has_many[$column]))
{
$model = ORM::factory($this->_has_many[$column]['model']);
if (isset($this->_has_many[$column]['through']))
{
// Grab has_many "through" relationship table
$through = $this->_has_many[$column]['through'];
// Join on through model's target foreign key (far_key) and target model's primary key
$join_col1 = $through.'.'.$this->_has_many[$column]['far_key'];
$join_col2 = $model->_table_name.'.'.$model->_primary_key;
$model->join($through)->on($join_col1, '=', $join_col2);
// Through table's source foreign key (foreign_key) should be this model's primary key
$col = $through.'.'.$this->_has_many[$column]['foreign_key'];
$val = $this->pk();
}
else
{
// Simple has_many relationship, search where target model's foreign key is this model's primary key
$col = $model->_table_name.'.'.$this->_has_many[$column]['foreign_key'];
$val = $this->pk();
}
return $model->where($col, '=', $val);
}
else
{
throw new Kohana_Exception('The :property property does not exist in the :class class',
array(':property' => $column, ':class' => get_class($this)));
}
}
/**
* Base set method - this should not be overridden.
*
* @param string column name
* @param mixed column value
* @return void
*/
public function __set($column, $value)
{
if ( ! isset($this->_object_name))
{
// Object not yet constructed, so we're loading data from a database call cast
$this->_cast_data[$column] = $value;
}
else
{
// Set the model's column to given value
$this->set($column, $value);
}
}
/**
* Handles setting of column
*
* @param string column name
* @param mixed column value
* @return void
*/
public function set($column, $value)
{
if (array_key_exists($column, $this->_ignored_columns))
{
// No processing for ignored columns, just store it
$this->_object[$column] = $value;
}
elseif (array_key_exists($column, $this->_object))
{
$this->_object[$column] = $value;
if (isset($this->_table_columns[$column]))
{
// Data has changed
$this->_changed[$column] = $column;
// Object is no longer saved
$this->_saved = FALSE;
}
}
elseif (isset($this->_belongs_to[$column]))
{
// Update related object itself
$this->_related[$column] = $value;
// Update the foreign key of this model
$this->_object[$this->_belongs_to[$column]['foreign_key']] = $value->pk();
$this->_changed[$column] = $this->_belongs_to[$column]['foreign_key'];
}
else
{
throw new Kohana_Exception('The :property: property does not exist in the :class: class',
array(':property:' => $column, ':class:' => get_class($this)));
}
return $this;
}
/**
* Set values from an array with support for one-one relationships. This method should be used
* for loading in post data, etc.
*
* @param array array of column => val
* @return ORM
*/
public function values($values)
{
foreach ($values as $column => $value)
{
if (array_key_exists($column, $this->_object) OR array_key_exists($column, $this->_ignored_columns))
{
// Property of this model
$this->set($column, $value);
}
elseif (isset($this->_belongs_to[$column]) OR isset($this->_has_one[$column]))
{
// Value is an array of properties for the related model
$this->_related[$column] = $value;
}
}
return $this;
}
/**
* Returns the values of this object as an array, including any related one-one
* models that have already been loaded using with()
*
* @return array
*/
public function as_array()
{
$object = array();
foreach ($this->_object as $column => $value)
{
// Call __get for any user processing
$object[$column] = $this->__get($column);
}
foreach ($this->_related as $column => $model)
{
// Include any related objects that are already loaded
$object[$column] = $model->as_array();
}
return $object;
}
/**
* Binds another one-to-one object to this model. One-to-one objects
* can be nested using 'object1:object2' syntax
*
* @param string target model to bind to
* @return void
*/
public function with($target_path)
{
if (isset($this->_with_applied[$target_path]))
{
// Don't join anything already joined
return $this;
}
// Split object parts
$aliases = explode(':', $target_path);
$target = $this;
foreach ($aliases as $alias)
{
// Go down the line of objects to find the given target
$parent = $target;
$target = $parent->_related($alias);
if ( ! $target)
{
// Can't find related object
return $this;
}
}
// Target alias is at the end
$target_alias = $alias;
// Pop-off top alias to get the parent path (user:photo:tag becomes user:photo - the parent table prefix)
array_pop($aliases);
$parent_path = implode(':', $aliases);
if (empty($parent_path))
{
// Use this table name itself for the parent path
$parent_path = $this->_table_name;
}
else
{
if( ! isset($this->_with_applied[$parent_path]))
{
// If the parent path hasn't been joined yet, do it first (otherwise LEFT JOINs fail)
$this->with($parent_path);
}
}
// Add to with_applied to prevent duplicate joins
$this->_with_applied[$target_path] = TRUE;
// Use the keys of the empty object to determine the columns
foreach (array_keys($target->_object) as $column)
{
$name = $target_path.'.'.$column;
$alias = $target_path.':'.$column;
// Add the prefix so that load_result can determine the relationship
$this->select(array($name, $alias));
}
if (isset($parent->_belongs_to[$target_alias]))
{
// Parent belongs_to target, use target's primary key and parent's foreign key
$join_col1 = $target_path.'.'.$target->_primary_key;
$join_col2 = $parent_path.'.'.$parent->_belongs_to[$target_alias]['foreign_key'];
}
else
{
// Parent has_one target, use parent's primary key as target's foreign key
$join_col1 = $parent_path.'.'.$parent->_primary_key;
$join_col2 = $target_path.'.'.$parent->_has_one[$target_alias]['foreign_key'];
}
// Join the related object into the result
$this->join(array($target->_table_name, $target_path), 'LEFT')->on($join_col1, '=', $join_col2);
return $this;
}
/**
* Initializes the Database Builder to given query type
*
* @param int Type of Database query
* @return ORM
*/
protected function _build($type)
{
// Construct new builder object based on query type
switch ($type)
{
case Database::SELECT:
$this->_db_builder = DB::select();
break;
case Database::UPDATE:
$this->_db_builder = DB::update($this->_table_name);
break;
case Database::DELETE:
$this->_db_builder = DB::delete($this->_table_name);
}
// Process pending database method calls
foreach ($this->_db_pending as $method)
{
$name = $method['name'];
$args = $method['args'];
$this->_db_applied[$name] = $name;
switch (count($args))
{
case 0:
$this->_db_builder->$name();
break;
case 1:
$this->_db_builder->$name($args[0]);
break;
case 2:
$this->_db_builder->$name($args[0], $args[1]);
break;
case 3:
$this->_db_builder->$name($args[0], $args[1], $args[2]);
break;
case 4:
$this->_db_builder->$name($args[0], $args[1], $args[2], $args[3]);
break;
default:
// Much slower...
call_user_func_array(array($this->_db_builder, $name), $args);
break;
}
}
return $this;
}
/**
* Finds and loads a single database row into the object.
*
* @chainable
* @param mixed primary key
* @return ORM
*/
public function find($id = NULL)
{
if ( ! empty($this->_load_with))
{
foreach ($this->_load_with as $alias)
{
// Bind auto relationships
$this->with($alias);
}
}
$this->_build(Database::SELECT);
if ($id !== NULL)
{
// Search for a specific column
$this->_db_builder->where($this->_table_name.'.'.$this->_primary_key, '=', $id);
}
return $this->_load_result(FALSE);
}
/**
* Finds multiple database rows and returns an iterator of the rows found.
*
* @chainable
* @return Database_Result
*/
public function find_all()
{
if ( ! empty($this->_load_with))
{
foreach ($this->_load_with as $alias)
{
// Bind auto relationships
$this->with($alias);
}
}
$this->_build(Database::SELECT);
return $this->_load_result(TRUE);
}
/**
* Loads a database result, either as a new record for this model, or as
* an iterator for multiple rows.
*
* @chainable
* @param bool return an iterator or load a single row
*/
protected function _load_result($multiple = FALSE)
{
$this->_db_builder->from($this->_table_name);
if ($multiple === FALSE)
{
// Only fetch 1 record
$this->_db_builder->limit(1);
}
// Select all columns by default
$this->_db_builder->select($this->_table_name.'.*');
if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
{
foreach ($this->_sorting as $column => $direction)
{
if (strpos($column, '.') === FALSE)
{
// Sorting column for use in JOINs
$column = $this->_table_name.'.'.$column;
}
$this->_db_builder->order_by($column, $direction);
}
}
if ($multiple === TRUE)
{
// Return database iterator casting to this object type
$result = $this->_db_builder->as_object(get_class($this))->execute($this->_db);
$this->reset();
return $result;
}
else
{
// Load the result as an associative array
$result = $this->_db_builder->as_assoc()->execute($this->_db);
$this->reset();
if ($result->count() === 1)
{
// Load object values
$this->_load_values($result->current());
}
else
{
// Clear the object, nothing was found
$this->clear();
}
return $this;
}
}
/**
* Loads an array of values into into the current object.
*
* @chainable
* @param array values to load
* @return ORM
*/
protected function _load_values(array $values)
{
if (array_key_exists($this->_primary_key, $values))
{
if ($values[$this->_primary_key] !== NULL)
{
// Flag as loaded and saved
$this->_loaded = $this->_saved = TRUE;
// Store primary key
$this->_primary_key_value = $values[$this->_primary_key];
}
else
{
// Not loaded or saved
$this->_loaded = $this->_saved = FALSE;
}
}
// Related objects
$related = array();
foreach ($values as $column => $value)
{
if (strpos($column, ':') === FALSE)
{
// Load the value to this model
$this->_object[$column] = $value;
}
else
{
// Column belongs to a related model
list ($prefix, $column) = explode(':', $column, 2);
$related[$prefix][$column] = $value;
}
}
if ( ! empty($related))
{
foreach ($related as $object => $values)
{
// Load the related objects with the values in the result
$this->_related($object)->_load_values($values);
}
}
return $this;
}
/**
* Rule definitions for validation
*
* @return array
*/
public function rules()
{
return array();
}
/**
* Filter definitions for validation
*
* @return array
*/
public function filters()
{
return array();
}
/**
* Callback definitions for validation
*
* @return array
*/
public function callbacks()
{
return array();
}
/**
* Label definitions for validation
*
* @return array
*/
public function labels()
{
return array();
}
/**
* Initializes validation rules, callbacks, filters, and labels
*
* @return void
*/
protected function _validate()
{
$this->_validate = Validate::factory($this->_object);
foreach ($this->rules() as $field => $rules)
{
$this->_validate->rules($field, $rules);
}
foreach ($this->filters() as $field => $filters)
{
$this->_validate->filters($field, $filters);
}
// Use column names by default for labels
$columns = array_keys($this->_table_columns);
// Merge user-defined labels
$labels = array_merge(array_combine($columns, $columns), $this->labels());
foreach ($labels as $field => $label)
{
$this->_validate->label($field, $label);
}
foreach ($this->callbacks() as $field => $callbacks)
{
foreach ($callbacks as $callback)
{
$this->_validate->callback($field, $callback);
}
}
}
/**
* Validates the current model's data
*
* @return boolean
*/
public function check()
{