GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Scalable Active Record implementation: Using Alternative to Join with Emphasis on Write-Through Cache
Clone URL: git://github.com/Jakobo/scar.git
rearchitected scar class, split out scar generic, commented files

all of the files now have comments in them, and the scar_generic object
was split out into its own file
Jakobo (author)
Fri Mar 14 12:52:50 -0700 2008
commit  d64110b8e0a5c491803768c2b3eff3c4b7f70877
tree    d214c02c5a9d8d4323e99b286dfd1e8e99bf2ab2
parent  ad1ddf182c7257f33c9c6c1078cacb5c0632b237
...
1
 
 
 
 
 
 
2
3
4
5
 
6
7
8
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1,8 +1,15 @@
0
 <?php
0
+
0
+/**
0
+ * SCAR Constants file
0
+ **/
0
+
0
+// Field Types
0
 define('TYPE_INT', 1);
0
 define('TYPE_STRING', 2);
0
 define('TYPE_INT_AUTOINCREMENT', 3);
0
 
0
+// Relation Types
0
 define('RELATION_HASONE', 1);
0
 define('RELATION_HASMANY', 2);
0
 define('RELATION_HASMANYMANY', 3);
...
1
2
 
 
 
3
 
 
 
 
 
 
4
 
 
 
 
 
 
 
5
6
7
 
 
 
 
 
 
8
9
10
...
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
0
@@ -1,10 +1,32 @@
0
 <?php
0
 
0
+/**
0
+ * Datastore Interface
0
+ **/
0
 interface SCAR_Datastore_Interface {
0
+ /**
0
+ * Sets an object into the datastore
0
+ * @param $klass string a class name
0
+ * @param $key string a unique identifier built from the class' primary key
0
+ * @param $value array an array of row-wise data
0
+ **/
0
     public function set($klass, $key, $value);
0
+
0
+ /**
0
+ * Gets an object from the datastore
0
+ * @param $klass string a class name
0
+ * @param $key string a unique identifier built from the class' primary key
0
+ * @return array
0
+ **/
0
     public function get($klass, $key);
0
 }
0
 
0
+/**
0
+ * SCAR Datastore
0
+ * designed for holding rowwise data for SCAR_Generic objects, keyed by class
0
+ * and primary key. Long term, this object can be extended or reimplemented
0
+ * to use memcache for write-through caching.
0
+ **/
0
 class SCAR_Datastore implements SCAR_Datastore_Interface {
0
     protected $data = array();
0
     protected $queries = array();
...
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
...
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
0
@@ -1,26 +1,56 @@
0
 <?php
0
 
0
+/**
0
+ * Repository for loading table/column data into SCAR_Generic Objects
0
+ * The SCAR Repository takes the load off of class development by scanning
0
+ * tables and building the one to many / many to one / one to one
0
+ * relationships. This data is stored into a local container and on demand
0
+ * is populated into a SCAR_Generic object.
0
+ **/
0
 class SCAR_Repository {
0
+
0
+ /**
0
+ * A collection of SCARs keyed by name
0
+ * @var array
0
+ **/
0
     protected $scars = array();
0
+
0
+ /**
0
+ * Is the repository initialized
0
+ * @var boolean
0
+ **/
0
     protected $is_initialized = false;
0
+
0
+ /**
0
+ * A configuration path to load DBs from
0
+ * @var string
0
+ **/
0
+ protected $config_path = null;
0
 
0
+ /**
0
+ * The name of the SCAR object
0
+ * Used in unit testing to change to a test SCAR
0
+ * @var string
0
+ **/
0
     public $SCAR = 'SCAR';
0
     
0
- public function __construct() {}
0
+ public function __construct($config_path = null) {
0
+ $this->config_path = $config_path;
0
+ }
0
     
0
- public function initialize($config_path = null) {
0
+ public function initialize() {
0
         if ($this->is_initialized) {
0
             return true;
0
         }
0
         $this->is_initialized = true;
0
         
0
         $db = array();
0
-
0
- if ($config_path === null) {
0
+
0
+ if ($this->config_path === null) {
0
             include SCAR_BASE.'config.php';
0
         }
0
         else {
0
- include $config_path;
0
+ include $this->config_path;
0
         }
0
 
0
         $keymap = array();
...
10
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
...
19
20
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
23
24
...
60
61
62
 
63
64
65
...
70
71
72
73
 
74
75
76
77
78
79
80
 
 
81
82
83
...
87
88
89
90
 
91
92
93
...
97
98
99
 
100
101
102
...
133
134
135
 
136
137
138
...
162
163
164
 
 
 
 
 
 
165
166
167
...
170
171
172
 
 
 
 
 
 
 
 
173
174
175
...
216
217
218
 
 
 
 
 
219
220
221
...
241
242
243
 
 
 
 
 
 
244
245
246
...
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
...
10
11
12
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
...
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
...
95
96
97
98
99
100
101
...
106
107
108
 
109
110
111
112
113
114
115
 
116
117
118
119
120
...
124
125
126
 
127
128
129
130
...
134
135
136
137
138
139
140
...
171
172
173
174
175
176
177
...
201
202
203
204
205
206
207
208
209
210
211
212
...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
...
269
270
271
272
273
274
275
276
277
278
279
...
299
300
301
302
303
304
305
306
307
308
309
310
...
335
336
337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -10,7 +10,22 @@ include SCAR_LIB . 'constants.php';
0
 include SCAR_LIB . 'sql.php';
0
 include SCAR_LIB . 'datastore.php';
0
 include SCAR_LIB . 'repository.php';
0
-
0
+include SCAR_LIB . 'scargeneric.php';
0
+
0
+/**
0
+ * SCAR Factory and Bridge Class
0
+ * The SCAR Object is a static object that:
0
+ * - creates new SCAR_Generic Objects
0
+ * - runs queries for SCAR_Generic Objects
0
+ * - can retrieve tables and columns for databases
0
+ * - can escape strings
0
+ * - serves as a bridge for multiple DB types
0
+ *
0
+ * It should be used as the start of any SCAR retrieval chain, using the format
0
+ * SCAR::get('tables') and from that point forward, you will be working with
0
+ * SCAR_Generic objects. To trigger an insert, use the format SCAR::make('object')
0
+ * to get a SCAR_Generic object geared towards inserting data.
0
+ **/
0
 class SCAR {
0
     /**
0
      * contains an array of timings for events as they happen
0
@@ -19,6 +34,26 @@ class SCAR {
0
     public static $timings = array();
0
     
0
     /**
0
+ * Specify a config path for the application or retrieve the config path
0
+ * @param $cfg string a string to set the config path to
0
+ * @return string
0
+ **/
0
+ public static function config($cfg = null) {
0
+ static $config;
0
+ if (!isset($config)) {
0
+ $config = null;
0
+ }
0
+
0
+ if ($cfg === null) {
0
+ return $config;
0
+ }
0
+
0
+ $config = $cfg;
0
+
0
+ return $config;
0
+ }
0
+
0
+ /**
0
      * Record a timestamp for benchmarking
0
      * if $finish is provided, then it will be the time lapsed since
0
      * mark was called last for $method without $finish.
0
@@ -60,6 +95,7 @@ class SCAR {
0
      * @param $name string a name of a table to create an object for
0
      * @param $columns string [optional] a list of columns to limit to
0
      * @return SCAR_Generic
0
+ * @throws SCAR_Object_Not_Found_Exception
0
      **/
0
     public static function get($name, $columns = null) {
0
         static $repository;
0
@@ -70,14 +106,15 @@ class SCAR {
0
         if (class_exists($klass)) {
0
             $k = new $klass();
0
             $k->columns($columns);
0
- $k->payload($datastore);
0
+ $k->datastore($datastore);
0
             return $k;
0
         }
0
         
0
         // class does not exist, check the repository
0
         if (!isset($repository)) {
0
             self::mark('repository');
0
- $repository = new SCAR_Repository();
0
+
0
+ $repository = new SCAR_Repository(SCAR::config());
0
             self::mark('repository', true);
0
         }
0
         
0
@@ -87,7 +124,7 @@ class SCAR {
0
             throw new SCAR_Object_Not_Found_Exception(Inflector::camelize($name));
0
         }
0
         $k->columns($columns);
0
- $k->payload($datastore);
0
+ $k->datastore($datastore);
0
         return $k;
0
     }
0
     
0
@@ -97,6 +134,7 @@ class SCAR {
0
      * can then call. This serves as a factory method, attempting to load
0
      * schemes out of the db/ directory
0
      * @param $scheme string The scheme type to load, ie 'mysql'
0
+ * @throws SCAR_Scheme_Not_Found_Exception
0
      **/
0
     public static function load($scheme) {
0
         $klass = 'SCAR_'.$scheme;
0
@@ -133,6 +171,7 @@ class SCAR {
0
      * Opens a connection to a database via a subscheme
0
      * @param $dsn string a scheme://user:pass@host/db formatted string
0
      * @return a connection object reference for that db combination
0
+ * @throws SCAR_Connection_Exception
0
      **/
0
     public static function connect($dsn) {
0
         static $connectors;
0
@@ -162,6 +201,12 @@ class SCAR {
0
         return $connectors[$hash];
0
     }
0
     
0
+ /**
0
+ * Calls a scheme specific escape function
0
+ * @param $dsn string a formatted scheme://user:pass@host/db string
0
+ * @param $string string what we want to escape for SQL
0
+ * @return string
0
+ **/
0
     public static function escape($dsn, $string) {
0
         $uri = parse_url($dsn);
0
 
0
@@ -170,6 +215,14 @@ class SCAR {
0
         return call_user_func_array(array('SCAR_'.$uri['scheme'], 'escape'), array($string));
0
     }
0
     
0
+ /**
0
+ * Runs a scheme specific query and normalizes the results for SCAR objects
0
+ * @param $dsn string a scheme://user:pass@host/db formatted string
0
+ * @param $sql string the SQL to run
0
+ * @param $pk_list array the primary key list for the object
0
+ * @return array
0
+ * @throws SCAR_Query_Exception
0
+ **/
0
     public static function query($dsn, $sql, $pk_list) {
0
         // var_dump($sql);
0
         self::mark($sql);
0
@@ -216,6 +269,11 @@ class SCAR {
0
         );
0
     }
0
     
0
+ /**
0
+ * Calls a scheme specific show tables command
0
+ * @param $dsn string a scheme://user:pass@host/db formatted string
0
+ * @return array
0
+ **/
0
     public static function showTables($dsn) {
0
         $db = SCAR::connect($dsn);
0
         $uri = parse_url($dsn);
0
@@ -241,6 +299,12 @@ class SCAR {
0
         return $results;
0
     }
0
     
0
+ /**
0
+ * Calls a scheme specific version of showing the columns in a database
0
+ * @param $dsn string a scheme://user:pass@host/db formatted string
0
+ * @param $table string a table name to get the columns for
0
+ * @return array
0
+ **/
0
     public static function showColumns($dsn, $table) {
0
         $db = SCAR::connect($dsn);
0
         $uri = parse_url($dsn);
0
@@ -271,613 +335,3 @@ class SCAR {
0
 
0
 
0
 
0
-class SCAR_Generic implements Iterator {
0
- protected $_keys = array();
0
- protected $_whoami = false;
0
- protected $_datastore = null;
0
- protected $_cksum = null;
0
- protected $_lastrs = null;
0
- protected $_filter = null;
0
- protected $_data = array(
0
- 'name' => null,
0
- 'dsn' => null,
0
- 'table' => null,
0
- 'fields' => array(),
0
- 'primary_key' => array(),
0
- 'columns' => array(),
0
- 'relations' => array(),
0
-
0
- 'where' => array(),
0
- 'set' => array(),
0
-
0
- 'scar' => 'SCAR',
0
- );
0
-
0
- public function SCAR($scar) { $this->_data['scar'] = $scar; return $this; }
0
- public function getSCAR() { return $this->_data['scar']; }
0
-
0
- public function dsn($dsn) { $this->_data['dsn'] = $dsn; return $this; }
0
- public function getDsn() { return $this->_data['dsn']; }
0
-
0
- public function name($name) { $this->_data['name'] = $name; return $this; }
0
- public function getName() { return ($this->_data['name']) ? $this->_data['name'] : get_class($this); }
0
-
0
- public function fields($fields) { $this->_data['fields'] = $fields; return $this; }
0
- public function getFields() { return $this->_data['fields']; }
0
-
0
- public function primaryKey() {
0
- $keys = func_get_args();
0
- $this->_data['primary_key'] = (is_array($keys[0])) ? $keys[0] : $keys;
0
- return $this;
0
- }
0
- public function getPrimaryKey() { return $this->_data['primary_key']; }
0
-
0
- public function table($table) { $this->_data['table'] = $table; return $this; }
0
- public function getTable() { return ($this->_data['table']) ? $this->_data['table'] : Inflector::tableize($this->getName()); }
0
-
0
- public function getWhere() { return $this->_data['where']; }
0
- public function getSet() { return $this->_data['set']; }
0
-
0
- public function payload(SCAR_Datastore_Interface $pl) { $this->_datastore = $pl; }
0
- public function getPayload() { return $this->_datastore; }
0
-
0
- public function iAm($key) { $this->_whoami = $key; return $this; }
0
- public function whoAmI() { return $this->_whoami; }
0
-
0
- public function columns($col) {
0
- if ($col === null) {
0
- $this->_data['columns'] = null;
0
- return $this;
0
- }
0
-
0
- $this->_data['columns'] = (is_array($col)) ? $col : array($col);
0
- return $this;
0
- }
0
- public function getColumns() { return $this->_data['columns']; }
0
-
0
- public function hasOne($what, $key_relation = null) { return $this->_has(RELATION_HASONE, $what, $key_relation); }
0
- public function hasMany($what, $key_relation = null) { return $this->_has(RELATION_HASMANY, $what, $key_relation); }
0
-
0
- // what is $klass relation to this class
0
- public function getRelation($klass) {
0
- $klass = strtolower($klass);
0
- return (isset($this->_data['relations'][$klass])) ? $this->_data['relations'][$klass] : false;
0
- }
0
-
0
- // save an object
0
- public function save() {
0
- $this->_doSQL();
0
- return $this;
0
- }
0
-
0
- public function __call($method, $args) {
0
- if (substr($method, 0, 2) == 'by') {
0
- array_unshift($args, Inflector::columnize(substr($method, 2)));
0
- return $this->_by($args);
0
- }
0
-
0
- return $this->_join($method);
0
- }
0
-
0
- // ----------------------------------------------------------------------
0
- // Magic Methods
0
- // ----------------------------------------------------------------------
0
-
0
- // byColumnName($col, $val);
0
- // byColumnName($col, $val_array);
0
- // byColumnName($compare, $col, $val);
0
- protected function _by($args) {
0
- // is $args[1] an object?
0
- if (is_object($args[1]) && $args[1] instanceof SCAR_Generic) {
0
-
0
- list($column, $scar) = $args;
0
- $relation = $this->getRelation($scar->getName());
0
-
0
- // if it has a relation, use a join
0
- if ($relation) {
0
- return ($this->_join($scar->getTable()));
0
- }
0
- else {
0
- // attempt to find the relation via intermediate table
0
- $relation_name = $this->getName().$scar->getName();
0
- $relation = $this->getRelation($relation_name);
0
- if (!$relation) {
0
- $relation_name = $scar->getName().$this->getName();
0
- $relation = $this->getRelation($relation_name);
0
- if (!$relation) {
0
- throw new SCAR_Relation_Exception($this->getName(), $scar->getName());
0
- }
0
- }
0
-
0
- // relation_name is the object we need
0
- // relation describes that relation
0
- $m = Inflector::tableize($relation_name);
0
- $results = $scar->$m();
0
-
0
- $value = array();
0
- foreach ($results as $result) {
0
- $remote = $relation['keys'][$column];
0
- $value[] = $result->$remote;
0
- }
0
- $operator = 'IN';
0
- }
0
-
0
- }
0
- else {
0
- // not a relational object, that makes life easier
0
- if (isset($args[2])) {
0
- list($column, $operator, $value) = $args;
0
- }
0
- else {
0
- list($column, $value) = $args;
0
- $operator = (is_array($value) && count($value) > 1) ? 'IN' : '=';
0
- }
0
- }
0
-
0
- // optimize for a single value array
0
- if (is_array($value) && count($value) == 1) {
0
- $value = $value[0];
0
- $operator = '=';
0
- }
0
-
0
- $this->_data['where'][] = array('col' => $column, 'oper' => $operator, 'value' => $value);
0
- return $this;
0
- }
0
-
0
- // setColumnName($value)
0
- protected function _set($args) {
0
- list($column, $value) = $args;
0
-
0
- $fields = $this->getFields();
0
- $type = $fields[$column];
0
-
0
- if ($type === TYPE_INT_AUTOINCREMENT) {
0
- // TODO exception
0
- }
0
-
0
- switch ($type) {
0
- case TYPE_INT:
0
- $value = (string)$value;
0
- $value = (ctype_digit($value)) ? $value : 0;
0
- break;
0
- case TYPE_STRING:
0
- default:
0
- }
0
-
0
- $this->_data['set'][] = array('col' => $column, 'value' => $value);
0
- return $this;
0
- }
0
-
0
- // newtables()
0
- protected function _join($table) {
0
- // get the class
0
- $klass = call_user_func_array(array($this->getSCAR(), 'get'), array($table));
0
- $klassname = $klass->getName();
0
-
0
- // get the relation
0
- $relation = $this->getRelation($klassname);
0
-
0
- if (!$relation) {
0
- // attempt to find relational table for Many Many
0
- $relation_name = $this->getName().$klass->getName();
0
- $relation = $this->getRelation($relation_name);
0
- if (!$relation) {
0
- $relation_name = $klass->getName().$this->getName();
0
- $relation = $this->getRelation($relation_name);
0
- if (!$relation) {
0
- throw new SCAR_Relation_Exception($this->getName(), $klassname);
0
- }
0
- }
0
-
0
- // relates via many/many
0
- $link_table = Inflector::tableize($relation_name);
0
- return $this->$link_table()->$table();
0
- }
0
-
0
- // join by SQL many records
0
- if (!$this->whoAmI()) {
0
- // do SQL up to now
0
- $rs = $this->_doSQL();
0
-
0
- foreach($relation['keys'] as $local_key => $foreign_key) {
0
- // find local key in key index
0
- $ids = array();
0
- foreach ($rs['keys'] as $keyspace => $match) {
0
- $keys = array_flip(explode(',', $keyspace));
0
- foreach ($match as $key_list) {
0
- $list = explode(',', $key_list);
0
- $ids[] = $list[$keys[$local_key]];
0
- }
0
- }
0
- // var_dump($ids);
0
- // if ($local_key == 'tag_id') { var_dump($relation); var_dump($rs); }
0
- // $ids = $rs['keys'][$local_key];
0
- $klass_method = Inflector::byMethodize($foreign_key);
0
-
0
- $klass->$klass_method($ids);
0
- }
0
- }
0
- // join by one to many records, need ID
0
- else {
0
- $id = $this->whoAmI();
0
- $row = $this->getPayload()->get($this->getName(), $id);
0
- $f_keys = array();
0
-
0
- foreach($relation['keys'] as $local_key => $foreign_key) {
0
- $f_keys[] = $row[$local_key];
0
- $klass_method = Inflector::byMethodize($foreign_key);
0
- $klass->$klass_method($row[$local_key]);
0
- }
0
-
0
- // does this thing already exist in our datastore? if so
0
- // we can iAm() it and it will live on its own
0
- $f_keys = implode(',', $f_keys);
0
-
0
- if ($this->getPayload()->get($klass->getName(), $f_keys)) {
0
- $klass->iAm($f_keys);
0
- }
0
- }
0
-
0
- // share the data storage love
0
- $klass->payload($this->getPayload());
0
-
0
- // now calls are on the new object
0
- return $klass;
0
- }
0
-
0
- // ----------------------------------------------------------------------
0
- // SQL Operations
0
- // ----------------------------------------------------------------------
0
- protected function _doSQL() {
0
- // things who know who they are don't need SQL
0
- if ($this->whoAmI()) {
0
- return true;
0
- }
0
-
0
- // table
0
- $table = $this->getTable();
0
- $table = '`'.$table.'`';
0
-
0
- // update
0
- if (count($this->getSet()) > 0 && $this->whoAmI()) {
0
- $sql = new SCAR_SQL('update', $this->getDSN(), $this->getSCAR());
0
- $sql->table = $this->getTable();
0
-
0
- // where by pkey
0
- $where = array();
0
- $values = explode(',', $this->whoAmI());
0
- $keys = $this->getPrimaryKey();
0
- foreach ($keys as $idx => $key) {
0
- $where[] = array('col' => $key, 'oper' => '=', 'value' => $values[$idx]);
0
- }
0
- $sql->where = $where;
0
-
0
- $sql->set = $this->getSet();
0
- $stmt = $sql->render();
0
-
0
- $rs = call_user_func_array(array($this->getSCAR(), 'query'), array($this->getDSN(), $stmt, $this->getPrimaryKey()));
0
-
0
- // okay, update payload
0
- if ($rs['affected'] === 1) {
0
- $row = $this->getPayload()->get($this->getName(), $this->whoAmI());
0
- foreach ($this->getSet() as $set_piece) {
0
- $row[$set_piece['col']] = $set_piece['value'];
0
- }
0
- $this->getPayload()->set($this->getName(), $this->whoAmI(), $row);
0
-
0
- return $this;
0
- }
0
-
0
- return false;
0
- }
0
- // insert
0
- elseif (count($this->getSet()) > 0) {
0
-
0
- $sql = new SCAR_SQL('insert', $this->getDSN(), $this->getSCAR());
0
- $sql->table = $this->getTable();
0
-
0
- $sql->set = $this->getSet();
0
- $stmt = $sql->render();
0
-
0
- $rs = call_user_func_array(array($this->getSCAR(), 'query'), array($this->getDSN(), $stmt, $this->getPrimaryKey()));
0
-
0
- if ($rs['affected'] === 1) {
0
- // get a new one and select it
0
- $id = $rs['next'];
0
-
0
- $klass = call_user_func_array(array($this->getSCAR(), 'get'), array($this->getName()));
0
-
0
- // change set to key/value pairs
0
- $set = array();
0
- foreach ($this->getSet() as $set_piece) {
0
- $set[$set_piece['col']] = $set_piece['value'];
0
-