Skip to content

Commit

Permalink
Proxying the __get and __isset methods from Association to Table
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 11, 2014
1 parent a37725f commit c3647a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ORM/Association.php
Expand Up @@ -648,6 +648,29 @@ protected function _joinCondition(array $options) {
return $conditions;
}

/**
* Proxies property retrieval to the target table. This is handy for getting this
* association's associations
*
* @param string $property the property name
* @return \Cake\ORM\Association
* @throws \RuntimeException if no association with such name exists
*/
public function __get($property) {
return $this->target()->{$property};
}

/**
* Proxies the isset call to the target table. This is handy to check if the
* target table has another association with the passed name
*
* @param string $property the property name
* @return boolean true if the property exists
*/
public function __isset($property) {
return isset($this->target()->{$property});
}

/**
* Get the relationship type.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/ORM/AssociationProxyTest.php
Expand Up @@ -91,4 +91,18 @@ public function testDeleteAllFromAssociation() {
$this->assertEquals(1, $remaining);
}

/**
* Tests that it is possible to get associations as a property
*
* @return void
*/
public function testAssociationAsPropertyProxy() {
$articles = TableRegistry::get('articles');
$authors = TableRegistry::get('authors');
$articles->belongsTo('authors');
$authors->hasMany('comments');
$this->assertTrue(isset($articles->authors->comments));
$this->assertSame($authors->association('comments'), $articles->authors->comments);
}

}

0 comments on commit c3647a2

Please sign in to comment.