root/trunk/application/models/ignitedrecord/relproperty.php
| 104 | 110 | ||
|---|---|---|---|
49 | var $table; | 49 | var $table; |
50 | 50 | ||
51 | /** | 51 | /** |
52 | * The model name to use when creating related objects. | ||
53 | */ | ||
54 | var $model; | ||
55 | | ||
56 | /** | ||
57 | * The column that the other column relates to. | 52 | * The column that the other column relates to. |
58 | */ | 53 | */ |
59 | var $fk; | 54 | var $fk; |
... | ... | ||
71 | { | 66 | { |
72 | $this->table = $settings['table']; | 67 | $this->table = $settings['table']; |
73 | } | 68 | } |
74 | if(isset($settings['model'])) | 69 | |
75 | { | ||
76 | $this->model = $settings['model']; | ||
77 | } | ||
78 | if(isset($settings['foreign_key'])) | 70 | if(isset($settings['foreign_key'])) |
79 | { | 71 | { |
80 | $this->fk = $settings['foreign_key']; | 72 | $this->fk = $settings['foreign_key']; |
81 | } | 73 | } |
74 | |||
82 | if(isset($settings['fk'])) | 75 | if(isset($settings['fk'])) |
83 | { | 76 | { |
84 | $this->fk = $settings['fk']; | 77 | $this->fk = $settings['fk']; |
... | ... | ||
113 | */ | 106 | */ |
114 | function singular($singular) | 107 | function singular($singular) |
115 | { | 108 | { |
116 | if( ! isset($this->model)) | 109 | $this->table = plural($singular); |
117 | $this->model = $singular; | ||
118 | } | 110 | } |
119 | 111 | ||
120 | // -------------------------------------------------------------------- | 112 | // -------------------------------------------------------------------- |
121 | 113 | ||
122 | /** | 114 | /** |
123 | * Sets the model name for this relation. | ||
124 | * | ||
125 | * @param $model The modelname | ||
126 | * @return $this | ||
127 | */ | ||
128 | function &model($model) | ||
129 | { | ||
130 | $this->model = $model; | ||
131 | return $this; | ||
132 | } | ||
133 | | ||
134 | // -------------------------------------------------------------------- | ||
135 | | ||
136 | /** | ||
137 | * Sets the table name for this relation. | 115 | * Sets the table name for this relation. |
138 | * | 116 | * |
139 | * @param $table The table name | 117 | * @param $table The table name |
... | ... | ||
183 | */ | 161 | */ |
184 | function get_table() | 162 | function get_table() |
185 | { | 163 | { |
186 | return isset($this->table) ? $this->table : plural($this->model); | 164 | return $this->table; |
187 | } | 165 | } |
188 | 166 | ||
189 | // -------------------------------------------------------------------- | 167 | // -------------------------------------------------------------------- |
190 | 168 | ||
191 | /** | 169 | /** |
192 | * Returns the model used by this relation. | ||
193 | * | ||
194 | * @return string | ||
195 | */ | ||
196 | function get_model() | ||
197 | { | ||
198 | if(isset($this->model)) | ||
199 | return $this->model; | ||
200 | $this->model = $this->_get_modelname($this->table); | ||
201 | return $this->model; | ||
202 | } | ||
203 | | ||
204 | // -------------------------------------------------------------------- | ||
205 | | ||
206 | /** | ||
207 | * Returns the foreign key used by this relation. | 170 | * Returns the foreign key used by this relation. |
208 | * | 171 | * |
209 | * @return string | 172 | * @return string |
... | ... | ||
212 | { | 175 | { |
213 | return isset($this->fk) | 176 | return isset($this->fk) |
214 | ? $this->fk | 177 | ? $this->fk |
215 | : ($this->get_model() == false | 178 | : (($name = $this->_get_modelname()) |
216 | ? singular($this->get_table()) | 179 | ? $name |
217 | : $this->get_model() | 180 | : singular($this->get_table()) |
218 | ).'_id'; | 181 | ).'_id'; |
219 | } | 182 | } |
220 | 183 | ||
221 | // -------------------------------------------------------------------- | 184 | // -------------------------------------------------------------------- |
222 | 185 | ||
223 | /** | 186 | /** |
224 | * Returns the modelname of the tablename. | 187 | * Returns the modelname of model linked to $tablename. |
225 | * | 188 | * |
226 | * Tries if a model with $tablename exists, then tries with singular form of the $tablename. \n | ||
227 | * Called if no model is defined for a table. | ||
228 | * | 189 | * |
229 | * @access private | 190 | * @access private |
230 | * @param $tablename The tablename to find a modelname of. | 191 | * @param $tablename The tablename |
231 | * @return The modelname or '' if no model is found | 192 | * @return The modelname or singlualr($tablename) if no model is found |
232 | */ | 193 | */ |
233 | function _get_modelname($tablename) | 194 | function _get_modelname($tablename) |
234 | { | 195 | { |
235 | $tablename = strtolower($tablename); | 196 | $tablename = strtolower($tablename); |
236 | $model = false; | ||
237 | 197 | ||
238 | // check the CI props for an initialized IR model with that name | 198 | if($model =& IR_base::get_model($tablename)) |
239 | $CI =& get_instance(); | ||
240 | | ||
241 | foreach(array(singular($tablename), $tablename, singular($tablename).'_model', $tablename.'_model') as $name) | ||
242 | { | 199 | { |
243 | if(isset($CI->$name) && (is_a($CI->$name,'IgnitedRecord') OR is_a($CI->$name,'Model'))) | 200 | $name = $model->_get_modelname(); |
244 | { | 201 | |
245 | $model = $name; | 202 | return empty($name) ? singular($tablename) : $name; |
246 | break; | ||
247 | } | ||
248 | if(file_exists(APPPATH.'/models/'.$name.EXT)) | ||
249 | { | ||
250 | $model = $name; | ||
251 | break; | ||
252 | } | ||
253 | } | 203 | } |
254 | 204 | else | |
255 | return $model; | 205 | { |
206 | return singular($tablename); | ||
207 | } | ||
256 | } | 208 | } |
257 | } | 209 | } |
258 | 210 | ||
... | ... | ||
266 | */ | 218 | */ |
267 | class IR_RelProperty_has extends IR_RelProperty{ | 219 | class IR_RelProperty_has extends IR_RelProperty{ |
268 | /** | 220 | /** |
269 | * The mode that utilizes this object. | 221 | * The model that utilizes this object. |
270 | * Used to get the default column name to relate with. | 222 | * Used to get the default column name to relate with. |
271 | */ | 223 | */ |
272 | var $parent_model; | 224 | var $parent_model; |
... | ... | ||
280 | */ | 232 | */ |
281 | function get_fk() | 233 | function get_fk() |
282 | { | 234 | { |
283 | return isset($this->fk) ? $this->fk : $this->parent_model->get_modelname().'_id'; | 235 | return isset($this->fk) |
236 | ? $this->fk | ||
237 | : (($name = $this->parent_model->_get_modelname()) | ||
238 | ? $name | ||
239 | : singular($this->table) | ||
240 | ) . '_id'; | ||
284 | } | 241 | } |
285 | } | 242 | } |
286 | 243 | ||
... | ... | ||
317 | function IR_RelProp_habtm($settings = array()) | 274 | function IR_RelProp_habtm($settings = array()) |
318 | { | 275 | { |
319 | parent::IR_RelProp($settings); | 276 | parent::IR_RelProp($settings); |
277 | |||
320 | foreach(array('related_foreign_key', 'related_fk', 'r_fk') as $key) | 278 | foreach(array('related_foreign_key', 'related_fk', 'r_fk') as $key) |
321 | { | 279 | { |
322 | if(isset($settings[$key])) | 280 | if(isset($settings[$key])) |
... | ... | ||
354 | */ | 312 | */ |
355 | function get_join_table() | 313 | function get_join_table() |
356 | { | 314 | { |
357 | return isset($this->join_table) ? $this->join_table : | 315 | return isset($this->join_table) |
358 | (strcmp($this->parent_table, $this->get_table()) < 0 ? $this->parent_table.'_'.$this->get_table() : $this->get_table().'_'.$this->parent_table); | 316 | ? $this->join_table |
317 | : (strcmp($this->parent_table, $this->get_table()) < 0 | ||
318 | ? $this->parent_table.'_'.$this->get_table() | ||
319 | : $this->get_table().'_'.$this->parent_table | ||
320 | ); | ||
359 | } | 321 | } |
360 | 322 | ||
361 | // -------------------------------------------------------------------- | 323 | // -------------------------------------------------------------------- |
... | ... | ||
409 | { | 371 | { |
410 | return isset($this->related_fk) | 372 | return isset($this->related_fk) |
411 | ? $this->related_fk | 373 | ? $this->related_fk |
412 | : ($this->get_model() == false | 374 | : (($name = $this->_get_modelname()) |
413 | ? singular($this->get_table()) | 375 | ? $name |
414 | : $this->get_model() | 376 | : singular($this->get_table()) |
415 | ).'_id'; | 377 | ).'_id'; |
416 | } | 378 | } |
417 | } | 379 | } |
Download diff