public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
More model relations flexibility.
vito (author)
Fri Nov 21 14:35:36 -0800 2008
commit  ccaa664167b47343121200bb89d9bfbbe8f7b5fb
tree    5a9439839c077abb09a5c17701800b99cc732b24
parent  58f22264d5b39d8b43ec52330dea960f2f109b4a
...
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
...
413
414
415
 
...
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
...
456
457
458
459
0
@@ -41,39 +41,82 @@
0
             $this->belongs_to = (array) $this->belongs_to;
0
             $this->has_many   = (array) $this->has_many;
0
             $this->has_one    = (array) $this->has_one;
0
+
0
             if (in_array($name, $this->belongs_to) or isset($this->belongs_to[$name])) {
0
                 $class = (isset($this->belongs_to[$name])) ? $this->belongs_to[$name] : $name ;
0
                 if (isset($this->belongs_to[$name])) {
0
                     $opts =& $this->belongs_to[$name];
0
                     $model = oneof(@$opts["model"], $name);
0
+
0
+                    if (preg_match("/^\(([a-z0-9_]+)\)$/", $model, $match))
0
+                        $model = $this->$match[1];
0
+
0
                     $match = oneof(@$opts["by"], strtolower($name));
0
-                    $where = array_merge(array("id" => $this->{$match."_id"}),
0
-                                         (array) @$opts["where"]);
0
+
0
+                    fallback($opts["where"], array("id" => $this->{$match."_id"}));
0
+
0
+                    $opts["where"] = (array) $opts["where"];
0
+                    foreach ($opts["where"] as &$val)
0
+                        if (preg_match("/^\(([a-z0-9_]+)\)$/", $val, $match))
0
+                            $val = $this->$match[1];
0
+
0
+                    fallback($opts["placeholders"], $placeholders);
0
                 } else {
0
                     $model = $name;
0
-                    $where = array("id" => $this->{$name."_id"});
0
+                    $opts = array("where" => array("id" => $this->{$name."_id"}));
0
                 }
0
 
0
-                return $this->$name = new $model(null, array("where" => $where));
0
+                return $this->$name = new $model(null, $opts);
0
             } elseif (in_array($name, $this->has_many) or isset($this->has_many[$name])) {
0
                 if (isset($this->has_many[$name])) {
0
                     $opts =& $this->has_many[$name];
0
-                    $model = oneof($opts["model"], $name);
0
+                    $model = oneof(@$opts["model"], depluralize($name));
0
+
0
+                    if (preg_match("/^\(([a-z0-9_]+)\)$/", $model, $match))
0
+                        $model = $this->$match[1];
0
+
0
                     $match = oneof(@$opts["by"], strtolower($name));
0
-                    $where = array_merge(array($match."_id" => $this->id),
0
-                                         (array) @$opts["where"]);
0
+
0
+                    fallback($opts["where"], array($match."_id" => $this->id));
0
+
0
+                    $opts["where"] = (array) $opts["where"];
0
+                    foreach ($opts["where"] as &$val)
0
+                        if (preg_match("/^\(([a-z0-9_]+)\)$/", $val, $match))
0
+                            $val = $this->$match[1];
0
+
0
+                    fallback($opts["placeholders"], $placeholders);
0
                 } else {
0
                     $model = depluralize($name);
0
                     $match = $model_name;
0
-                    $where = array(strtolower($match)."_id" => $this->id);
0
+                    $opts = array("where" => array(strtolower($match)."_id" => $this->id),
0
+                                  "placeholders" => $placeholders);
0
                 }
0
 
0
                 return $this->$name = call_user_func(array($model, "find"),
0
-                                                     array("where" => $where,
0
-                                                           "placeholders" => $placeholders));
0
+                                                     $opts);
0
             } elseif (in_array($name, $this->has_one)) {
0
-                $class = depluralize($name);
0
-                return $this->$name = new $class(null, array("where" => array(strtolower($model_name)."_id" => $this->id)));
0
+                if (isset($this->has_one[$name])) {
0
+                    $opts =& $this->has_one[$name];
0
+                    $model = oneof(@$opts["model"], depluralize($name));
0
+
0
+                    if (preg_match("/^\(([a-z0-9_]+)\)$/", $model, $match))
0
+                        $model = $this->$match[1];
0
+
0
+                    $match = oneof(@$opts["by"], strtolower($name));
0
+
0
+                    fallback($opts["where"], array($match."_id" => $this->id));
0
+
0
+                    $opts["where"] = (array) $opts["where"];
0
+                    foreach ($opts["where"] as &$val)
0
+                        if (preg_match("/^\(([a-z0-9_]+)\)$/", $val, $match))
0
+                            $val = $this->$match[1];
0
+                } else {
0
+                    $model = depluralize($name);
0
+                    $match = $model_name;
0
+                    $opts = array("where" => array(strtolower($match)."_id" => $this->id));
0
+                }
0
+
0
+                return $this->$name = new $model(null, $opts);
0
             }
0
         }
0
 
0
@@ -413,3 +456,4 @@
0
             echo $before.'<a href="'.Config::current()->chyrp_url.'/admin/?action=delete_'.$name.'&amp;id='.$this->id.'" title="Delete" class="'.($classes ? $classes." " : '').$name.'_delete_link delete_link" id="'.$name.'_delete_'.$this->id.'">'.$text.'</a>'.$after;
0
         }
0
     }
0
+

Comments