3737
3838namespace CodeIgniter \RESTful ;
3939
40- use CodeIgniter \API \ResponseTrait ;
4140use CodeIgniter \Controller ;
4241use CodeIgniter \HTTP \RequestInterface ;
4342use CodeIgniter \HTTP \ResponseInterface ;
@@ -70,17 +69,7 @@ public function initController(RequestInterface $request, ResponseInterface $res
7069 parent ::initController ($ request , $ response , $ logger );
7170
7271 // instantiate our model, if needed
73- if (! empty ($ this ->modelName ))
74- {
75- try
76- {
77- $ this ->model = $ this ->modelName ();
78- }
79- catch (\Exception $ e )
80- {
81- // ignored. we just own't use a model for now
82- }
83- }
72+ $ this ->setModel ($ this ->modelName );
8473 }
8574
8675 //--------------------------------------------------------------------
@@ -118,7 +107,8 @@ public function new()
118107 }
119108
120109 /**
121- * Process the creation/insertion of a new resource object
110+ * Process the creation/insertion of a new resource object.
111+ * This should be a POST.
122112 *
123113 * @return string
124114 */
@@ -161,7 +151,8 @@ public function edit($id = null)
161151 }
162152
163153 /**
164- * Process the updating, full or partial, of a specific resource object
154+ * Process the updating, full or partial, of a specific resource object.
155+ * This should be a POST.
165156 *
166157 * @param type $id
167158 * @return string
@@ -174,13 +165,40 @@ public function update($id = null)
174165 //--------------------------------------------------------------------
175166
176167 /**
177- * Set/change the model that this controller is bound to
168+ * Set or change the model this controller is bound to.
169+ * Given either the name or the object, determine the other.
178170 *
179- * @param type $which
171+ * @param string|object $which
180172 */
181173 public function setModel ($ which = null )
182174 {
183- $ this ->model = $ model ;
175+ // save what we have been given
176+ if (! empty ($ which ))
177+ {
178+ if (is_object ($ which ))
179+ {
180+ $ this ->model = $ which ;
181+ }
182+ else
183+ {
184+ $ this ->modelName = $ which ;
185+ }
186+ }
187+
188+ // make a model object if needed
189+ if (empty ($ this ->model ) && ! empty ($ this ->modelName ))
190+ {
191+ if (class_exists ($ this ->modelName ))
192+ {
193+ $ this ->model = new $ this ->modelName ;
194+ }
195+ }
196+
197+ // determine model name if needed
198+ if (empty ($ this ->modelName ) && ! empty ($ this ->model ))
199+ {
200+ $ this ->modelName = get_class ($ this ->model );
201+ }
184202 }
185203
186204}
0 commit comments