Skip to content

Commit

Permalink
Make unlink() return true.
Browse files Browse the repository at this point in the history
Having this method return true allows it to be used in conditionals.
While it never returns false naturally, returning true makes this method
more consistent with other methods like link() and replaceLinks().

Refs #9208
  • Loading branch information
markstory committed Aug 6, 2016
1 parent c54b7d5 commit e5795f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -709,12 +709,12 @@ protected function _saveLinks(EntityInterface $sourceEntity, $targetEntities, $o
* `$article->get('tags')` will contain all tags in `$newTags` after liking
*
* @param \Cake\Datasource\EntityInterface $sourceEntity the row belonging to the `source` side
* of this association
* of this association
* @param array $targetEntities list of entities belonging to the `target` side
* of this association
* of this association
* @param array $options list of options to be passed to the internal `save` call
* @throws \InvalidArgumentException when any of the values in $targetEntities is
* detected to not be already persisted
* detected to not be already persisted
* @return bool true on success, false otherwise
*/
public function link(EntityInterface $sourceEntity, array $targetEntities, array $options = [])
Expand Down Expand Up @@ -759,14 +759,14 @@ function () use ($sourceEntity, $targetEntities, $options) {
* `$article->get('tags')` will contain only `[$tag4]` after deleting in the database
*
* @param \Cake\Datasource\EntityInterface $sourceEntity an entity persisted in the source table for
* this association
* this association
* @param array $targetEntities list of entities persisted in the target table for
* this association
* this association
* @param array|bool $options list of options to be passed to the internal `delete` call,
* or a `boolean`
* or a `boolean`
* @throws \InvalidArgumentException if non persisted entities are passed or if
* any of them is lacking a primary key value
* @return void
* any of them is lacking a primary key value
* @return bool Success
*/
public function unlink(EntityInterface $sourceEntity, array $targetEntities, $options = [])
{
Expand All @@ -792,7 +792,7 @@ function () use ($sourceEntity, $targetEntities, $options) {

$existing = $sourceEntity->get($property) ?: [];
if (!$options['cleanProperty'] || empty($existing)) {
return;
return true;
}

$storage = new SplObjectStorage;
Expand All @@ -808,6 +808,7 @@ function () use ($sourceEntity, $targetEntities, $options) {

$sourceEntity->set($property, array_values($existing));
$sourceEntity->dirty($property, false);
return true;
}

/**
Expand Down Expand Up @@ -991,12 +992,12 @@ protected function _appendJunctionJoin($query, $conditions)
* `$article->get('tags')` will contain only `[$tag1, $tag3]` at the end
*
* @param \Cake\Datasource\EntityInterface $sourceEntity an entity persisted in the source table for
* this association
* this association
* @param array $targetEntities list of entities from the target table to be linked
* @param array $options list of options to be passed to the internal `save`/`delete` calls
* when persisting/updating new links, or deleting existing ones
* when persisting/updating new links, or deleting existing ones
* @throws \InvalidArgumentException if non persisted entities are passed or if
* any of them is lacking a primary key value
* any of them is lacking a primary key value
* @return bool success
*/
public function replaceLinks(EntityInterface $sourceEntity, array $targetEntities, array $options = [])
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -518,7 +518,7 @@ public function testUnlinkSuccess()
$initial = $entity->tags;
$this->assertCount(1, $initial);

$assoc->unlink($entity, $entity->tags);
$this->assertTrue($assoc->unlink($entity, $entity->tags));
$this->assertEmpty($entity->get('tags'), 'Property should be empty');

$new = $articles->get(2, ['contain' => 'Tags']);
Expand Down Expand Up @@ -549,7 +549,7 @@ public function testUnlinkWithoutPropertyClean()
$initial = $entity->tags;
$this->assertCount(1, $initial);

$assoc->unlink($entity, $initial, ['cleanProperty' => false]);
$this->assertTrue($assoc->unlink($entity, $initial, ['cleanProperty' => false]));
$this->assertNotEmpty($entity->get('tags'), 'Property should not be empty');
$this->assertEquals($initial, $entity->get('tags'), 'Property should be untouched');

Expand Down

0 comments on commit e5795f3

Please sign in to comment.