Browse Source

Proxying method calls from the Association class to the target table

Jose Lorenzo Rodriguez 12 years ago
parent
commit
3aa25dd498
2 changed files with 29 additions and 0 deletions
  1. 12 0
      src/ORM/Association.php
  2. 17 0
      tests/TestCase/ORM/AssociationProxyTest.php

+ 12 - 0
src/ORM/Association.php

@@ -672,6 +672,18 @@ abstract class Association {
 	}
 
 /**
+ * Proxies method calls to the target table.
+ *
+ * @param string $method name of the method to be invoked
+ * @param array $args List of arguments passed to the function
+ * @return mixed
+ * @throws \BadMethodCallException
+ */
+	public function __call($method, $argument) {
+		return call_user_func_array([$this->target(), $method], $argument);
+	}
+
+/**
  * Get the relationship type.
  *
  * @return string Constant of either ONE_TO_ONE, MANY_TO_ONE, ONE_TO_MANY or MANY_TO_MANY.

+ 17 - 0
tests/TestCase/ORM/AssociationProxyTest.php

@@ -105,4 +105,21 @@ class AssociationProxyTest extends TestCase {
 		$this->assertSame($authors->association('comments'), $articles->authors->comments);
 	}
 
+/**
+ * Tests that methods are proxied from the Association to the target table
+ *
+ * @return void
+ */
+	public function testAssociationMethodProxy() {
+		$articles = TableRegistry::get('articles');
+		$mock = $this->getMock('Cake\ORM\Table', ['crazy']);
+		$articles->belongsTo('authors', [
+			'targetTable' => $mock
+		]);
+
+		$mock->expects($this->once())->method('crazy')
+			->with('a', 'b')
+			->will($this->returnValue('thing'));
+		$this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
+	}
 }