Skip to content

Commit

Permalink
Deprecated Table::association()
Browse files Browse the repository at this point in the history
  • Loading branch information
sdustinh committed Oct 26, 2017
1 parent 66bbca2 commit 2784f16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
30 changes: 23 additions & 7 deletions src/ORM/Table.php
Expand Up @@ -871,28 +871,44 @@ public function hasBehavior($name)
return $this->_behaviors->has($name);
}

/**
* Returns an association object configured for the specified alias if any
*
* @deprecated 3.6.0 Use getAssociation() instead.
* @param string $name the alias used for the association.
* @return \Cake\ORM\Association|null Either the association or null.
*/
public function association($name)
{
return $this->getAssociation($name);
}

/**
* Returns an association object configured for the specified alias if any.
*
* The name argument also supports dot syntax to access deeper associations.
*
* ```
* $users = $this->association('Articles.Comments.Users');
* $users = $this->getAssociation('Articles.Comments.Users');
* ```
*
* @param string $name the alias used for the association.
* @return \Cake\ORM\Association|null Either the association or null.
*/
public function association($name)
public function getAssociation($name)
{
list($name, $next) = array_pad(explode('.', $name, 2), 2, null);
$result = $this->_associations->get($name);
if (strpos($name, '.') !== false) {
list($name, $next) = array_pad(explode('.', $name, 2), 2, null);
$result = $this->_associations->get($name);

if ($result !== null && $next !== null) {
$result = $result->getTarget()->association($next);
}

if ($result !== null && $next !== null) {
$result = $result->getTarget()->association($next);
return $result;
}

return $result;
return $this->_associations->get($name);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -511,7 +511,7 @@ function (Event $event, $query, $options) use ($expected) {
}

/**
* Test that the association() method supports the dot syntax.
* Test that the getAssociation() method supports the dot syntax.
*
* @return void
*/
Expand All @@ -526,10 +526,10 @@ public function testAssociationDotSyntax()
$groupsMembers->belongsTo('Members');
$members->belongsToMany('Groups');

$association = $groups->association('GroupsMembers.Members.Groups');
$this->assertInstanceOf('Cake\ORM\Association\BelongsToMany', $association);
$association = $groups->getAssociation('GroupsMembers.Members.Groups');
$this->assertInstanceOf(BelongsToMany::class, $association);
$this->assertSame(
$groups->association('GroupsMembers')->association('Members')->association('Groups'),
$groups->getAssociation('GroupsMembers')->getAssociation('Members')->getAssociation('Groups'),
$association
);
}
Expand Down

0 comments on commit 2784f16

Please sign in to comment.