Skip to content
This repository
Browse code

Merge pull request #744 from jails/pr3

Delegates the "magic call strategy" on model instance to `data\model\Model`.
  • Loading branch information...
commit f96a3ca8becdef1c0cc3361421248aae06629e5c 2 parents 6cc895b + e73ffe6
Nate Abele nateabele authored
18 data/Entity.php
@@ -182,25 +182,17 @@ public function __isset($name) {
182 182 * $record->validates();
183 183 * }}}
184 184 *
185   - * @see lithium\data\Model::instanceMethods
186   - * @param string $method
187   - * @param array $params
  185 + * @param string $method Method name caught by `__call()`.
  186 + * @param array $params Arguments given to the above `$method` call.
188 187 * @return mixed
189 188 */
190 189 public function __call($method, $params) {
191 190 if ($model = $this->_model) {
192   - $methods = $model::instanceMethods();
193 191 array_unshift($params, $this);
194   -
195   - if (method_exists($model, $method)) {
196   - $class = $model::invokeMethod('_object');
197   - return call_user_func_array(array(&$class, $method), $params);
198   - }
199   - if (isset($methods[$method]) && is_callable($methods[$method])) {
200   - return call_user_func_array($methods[$method], $params);
201   - }
  192 + $class = $model::invokeMethod('_object');
  193 + return call_user_func_array(array(&$class, $method), $params);
202 194 }
203   - $message = "No model bound or unhandled method call `{$method}`.";
  195 + $message = "No model bound to call `{$method}`.";
204 196 throw new BadMethodCallException($message);
205 197 }
206 198
18 data/Model.php
@@ -506,6 +506,24 @@ public static function __callStatic($method, $params) {
506 506 }
507 507
508 508 /**
  509 + * Magic method that allows calling `Model::_instanceMethods`'s closure like normal methods
  510 + * on the model instance.
  511 + *
  512 + * @see lithium\data\Model::instanceMethods
  513 + * @param string $method Method name caught by `__call()`.
  514 + * @param array $params Arguments given to the above `$method` call.
  515 + * @return mixed
  516 + */
  517 + public function __call($method, $params) {
  518 + $methods = static::instanceMethods();
  519 + if (isset($methods[$method]) && is_callable($methods[$method])) {
  520 + return call_user_func_array($methods[$method], $params);
  521 + }
  522 + $message = "Unhandled method call `{$method}`.";
  523 + throw new BadMethodCallException($message);
  524 + }
  525 +
  526 + /**
509 527 * The `find` method allows you to retrieve data from the connected data source.
510 528 *
511 529 * Examples:
9 tests/cases/data/EntityTest.php
@@ -91,7 +91,14 @@ public function testMethodDispatch() {
91 91 }));
92 92 $this->assertEqual('testInstanceMethod', $entity->testInstanceMethod($entity));
93 93
94   - $this->expectException("/^No model bound or unhandled method call `foo`.$/");
  94 + $this->expectException("/^Unhandled method call `foo`.$/");
  95 + $entity->foo();
  96 + }
  97 +
  98 + public function testMethodDispatchWithNoModel() {
  99 + $data = array('foo' => true);
  100 + $entity = new Entity(compact('data'));
  101 + $this->expectException("/^No model bound to call `foo`.$/");
95 102 $entity->foo();
96 103 }
97 104
2  tests/cases/data/entity/DocumentTest.php
@@ -445,7 +445,7 @@ public function testArrayValueSet() {
445 445 public function testInvalidCall() {
446 446 $doc = new Document();
447 447
448   - $this->expectException("No model bound or unhandled method call `medicin`.");
  448 + $this->expectException("No model bound to call `medicin`.");
449 449 $result = $doc->medicin();
450 450 $this->assertNull($result);
451 451 }
2  tests/cases/data/entity/RecordTest.php
@@ -120,7 +120,7 @@ public function testMethodDispatch() {
120 120 $this->assertEqual('create', $result['query']->type());
121 121 $this->assertEqual(array('title' => 'foo'), $result['query']->data());
122 122
123   - $this->expectException("No model bound or unhandled method call `invalid`.");
  123 + $this->expectException("Unhandled method call `invalid`.");
124 124 $this->assertNull($this->record->invalid());
125 125 }
126 126 }

0 comments on commit f96a3ca

Please sign in to comment.
Something went wrong with that request. Please try again.