Browse Source

Avoiding notice when unlinking values using HasMany

Fixes #8164
Jose Lorenzo Rodriguez 10 years ago
parent
commit
bc64a614b5
2 changed files with 20 additions and 1 deletions
  1. 2 1
      src/ORM/Association/HasMany.php
  2. 18 0
      tests/TestCase/ORM/TableTest.php

+ 2 - 1
src/ORM/Association/HasMany.php

@@ -306,7 +306,8 @@ class HasMany extends Association
 
         $this->_unlink($foreignKey, $target, $conditions, $options);
 
-        if ($options['cleanProperty']) {
+        $result = $sourceEntity->get($property);
+        if ($options['cleanProperty'] && $result !== null) {
             $sourceEntity->set(
                 $property,
                 (new Collection($sourceEntity->get($property)))

+ 18 - 0
tests/TestCase/ORM/TableTest.php

@@ -4126,6 +4126,24 @@ class TableTest extends TestCase
         $this->assertFalse($author->dirty('articles'));
     }
 
+    /**
+     * Integration test for unlinking entities with HasMany.
+     * Checking that no error happens when the hasMany property is originally
+     * null
+     *
+     * @return void
+     */
+    public function testUnlinkHasManyEmpty()
+    {
+        $authors = TableRegistry::get('Authors');
+        $articles = TableRegistry::get('Articles');
+        $authors->hasMany('Articles');
+        $author = $authors->get(1);
+        $article = $authors->Articles->get(1);
+
+        $authors->Articles->unlink($author, [$article]);
+    }
+
 
     /**
      * Integration test for replacing entities which depend on their source entity with HasMany and failing transaction. False should be returned when