public
Description: Scalable Active Record implementation: Using Alternative to Join with Emphasis on Write-Through Cache
Clone URL: git://github.com/Jakobo/scar.git
ActsAsMaterializedPath support added, basic nesting framework in place
Jakobo (author)
Thu May 15 03:10:16 -0700 2008
commit  ae0c9a755d59771ebcf56b5e694b60911de6916e
tree    93f70e2ce30d08f25b9b86dc0a8b21226aa0e1ac
parent  3b7ae8e6a592a2008f20583a819e7449fc5f8a36
...
1
2
3
4
5
 
 
 
 
 
6
...
1
2
3
 
4
5
6
7
8
9
10
0
@@ -1,4 +1,8 @@
0
 <?php
0
 
0
 // databases, list each one as a scheme://user:pass@host/db format
0
-// $db[] = 'mysql://root:root@localhost/test';
0
\ No newline at end of file
0
+// $db[] = 'mysql://root:root@localhost/test';
0
+
0
+// $properties['Class'] = array(
0
+// ACTS_AS_MATERIALIZED_PATH => array('column'),
0
+// );
0
\ No newline at end of file
...
13
14
15
 
 
 
16
...
13
14
15
16
17
18
19
0
@@ -13,3 +13,6 @@ define('TYPE_INT_AUTOINCREMENT', 3);
0
 define('RELATION_HASONE', 1);
0
 define('RELATION_HASMANY', 2);
0
 define('RELATION_HASMANYMANY', 3);
0
+
0
+// acts as
0
+define('ACTS_AS_MATERIALIZED_PATH', 1);
0
\ No newline at end of file
...
56
57
58
 
59
60
61
62
63
 
64
65
66
...
97
98
99
100
 
101
102
103
...
119
120
121
 
 
 
 
 
 
 
 
 
 
 
 
122
123
124
...
56
57
58
59
60
61
62
63
 
64
65
66
67
...
98
99
100
 
101
102
103
104
...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
0
@@ -56,11 +56,12 @@ class SCAR_Repository {
0
         $this->is_initialized = true;
0
         
0
         $db = array();
0
+ $properties = array();
0
 
0
         if ($this->config_path === null && count($this->db_list) == 0) {
0
             include SCAR_BASE.'config.php';
0
         }
0
- elseif ($this->config_path === null) {
0
+ elseif ($this->config_path !== null) {
0
             include $this->config_path;
0
         }
0
         else {
0
@@ -97,7 +98,7 @@ class SCAR_Repository {
0
                                       'relates' => Inflector::camelize(substr($col, 0, -3)),
0
                     );
0
                 }
0
-
0
+
0
                 // store
0
                 $this->scars[$klass_name] = $scar;
0
             }
0
@@ -119,6 +120,18 @@ class SCAR_Repository {
0
             // converse is also true
0
             $p->hasMany(Inflector::singularize(Inflector::classify($o->getTable())));
0
         }
0
+
0
+ // add properties
0
+ foreach ($properties as $klass => $props) {
0
+ $scar =& $this->scars[$klass];
0
+ foreach ($props as $type => $params) {
0
+ switch ($type) {
0
+ case ACTS_AS_MATERIALIZED_PATH:
0
+ $scar->actsAsMaterializedPath($params[0]);
0
+ break;
0
+ }
0
+ }
0
+ }
0
     }
0
     
0
     public function get($klass) {
...
61
62
63
 
 
64
65
66
...
276
277
278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
280
281
...
61
62
63
64
65
66
67
68
...
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
0
@@ -61,6 +61,8 @@ class SCAR_Generic implements Iterator {
0
         'set' => array(),
0
         
0
         'scar' => 'SCAR',
0
+
0
+ 'acts_as_materialized_path' => false,
0
     );
0
 
0
 
0
@@ -276,6 +278,101 @@ class SCAR_Generic implements Iterator {
0
         return (isset($this->_data['relations'][$klass])) ? $this->_data['relations'][$klass] : false;
0
     }
0
     
0
+ /**
0
+ * Says that this object acts with the Materialized Path pattern
0
+ * enables the calls for hasChildren and getChildren()
0
+ * @param string $col the column that contains path
0
+ * @return SCAR_Generic
0
+ **/
0
+ public function actsAsMaterializedPath($col) {
0
+ $this->_data['acts_as_materialized_path'] = $col;
0
+ }
0
+
0
+ /**
0
+ * Says a boolean if this object supports children or not
0
+ * @return boolean
0
+ **/
0
+ public function hasChildren() {
0
+ return ($this->_data['acts_as_materialized_path']) ? true : false;
0
+ }
0
+
0
+ /**
0
+ * get the children for this object based on the model type
0
+ * @return mixed SCAR_Generic or array()
0
+ **/
0
+ public function getChildren() {
0
+ if (!$this->hasChildren()) {
0
+ return array();
0
+ }
0
+
0
+ // do sql
0
+ $this->doSQL();
0
+
0
+ // if we don't know who we are, throw an exception
0
+ // can't multiplex children
0
+ if (!$this->whoAmI()) {
0
+ throw new SCAR_Method_Call_Exception('getChildren', $this);
0
+ }
0
+
0
+ if ($this->_data['acts_as_materialized_path']) {
0
+ $scar = call_user_func_array(array($this->getSCAR(), 'get'), array($this->getName()));
0
+ $call = 'by' . Inflector::Camelize($this->_data['acts_as_materialized_path']);
0
+ $scar->$call('LIKE', $this->path.'%');
0
+ return $scar;
0
+ }
0
+ }
0
+
0
+ /**
0
+ * looks at criteria to determine if a givnn object can have a parent
0
+ * @return boolean
0
+ **/
0
+ public function hasParent() {
0
+ if (!$this->_data['acts_as_materialized_path']) {
0
+ return false;
0
+ }
0
+
0
+ // do SQL
0
+ $this->doSQL();
0
+
0
+ // multiplex or 0 rows not valid
0
+ if (!$this->whoAmI()) {
0
+ return false;
0
+ }
0
+
0
+ if (strrpos($this->path, '/', -2) === 0) {
0
+ return false;
0
+ }
0
+
0
+ return true;
0
+ }
0
+
0
+ /**
0
+ * get the parent for this object based on the nesting model types
0
+ * @return SCAR_Generic or false
0
+ **/
0
+ public function getParent() {
0
+ if (!$this->hasParent()) {
0
+ return false;
0
+ }
0
+
0
+ // do sql
0
+ $this->doSQL();
0
+
0
+ // if we don't know who we are, throw an exception
0
+ // can't multiplex children
0
+ if (!$this->whoAmI()) {
0
+ throw new SCAR_Method_Call_Exception('getParent', $this);
0
+ }
0
+
0
+ if ($this->_data['acts_as_materialized_path']) {
0
+ $scar = call_user_func_array(array($this->getSCAR(), 'get'), array($this->getName()));
0
+ $call = 'by' . Inflector::Camelize($this->_data['acts_as_materialized_path']);
0
+ $path = substr($this->path, 0, strrpos($this->path, '/', -2) + 1);
0
+ var_dump($this->id.' '.$path);
0
+ $scar->$call($path); // path minus last node
0
+ return $scar;
0
+ }
0
+ }
0
 
0
     /**
0
      * Saves a SCAR object into the database

Comments

    No one has commented yet.