Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-849

PersistentCollection::clear() doesn't clear the collection

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0.7
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      PHP 5.3.2
      Doctrine 2 ORM soure codes form GitHub (2010-10-31)

      Description

      Clearing ManyToMany association doesn't work. I needed to clear the roles of the user (after form submit) and then add the checked roles. Also counting them doesn't work properly.

      See the clearRoles method in the attachment.

      //EDIT: Don't know if it's important but the association is bidirectional.

      1. User.php
        1 kB
        Jáchym Toušek

        Issue Links

          Activity

          Hide
          Roman S. Borschel added a comment -

          Can you provide more context, like the code that is using the entity? Thanks.

          Show
          Roman S. Borschel added a comment - Can you provide more context, like the code that is using the entity? Thanks.
          Hide
          Jáchym Toušek added a comment - - edited

          Code is something like this:

          <?php
          //$user is instance of UserEntity from the attachment
          $user->clearRoles();
          
          //$role is instance of RoleEntity which is in bidirectional association wirth user
          //$role sometimes is one of those previously cleared (assume it is for this example)
          if (...) {
              $user->addRole($role);
          /* which calles this:
              public function addRole(RoleEntity $entity) {
                  //contains method is false positive (the entity was removed by clear, but contains still returns TRUE)
                  if (!$this->roles->contains($entity)) {
                      $this->roles[] = $entity;
                  }
                  return $this;
              }
          */
          }
          
          //$em is instance of EntityManager
          $em->flush();
          ?>
          

          Ultimately count method returns wrong number and contains is false positive. I know that when i don't use the contains condition it works but that's not the point.

          Show
          Jáchym Toušek added a comment - - edited Code is something like this: <?php //$user is instance of UserEntity from the attachment $user->clearRoles(); //$role is instance of RoleEntity which is in bidirectional association wirth user //$role sometimes is one of those previously cleared (assume it is for this example) if (...) { $user->addRole($role); /* which calles this : public function addRole(RoleEntity $entity) { //contains method is false positive (the entity was removed by clear, but contains still returns TRUE) if (!$ this ->roles->contains($entity)) { $ this ->roles[] = $entity; } return $ this ; } */ } //$em is instance of EntityManager $em->flush(); ?> Ultimately count method returns wrong number and contains is false positive. I know that when i don't use the contains condition it works but that's not the point.
          Hide
          Benjamin Eberlei added a comment -

          Sorry, but isn't then PersistentCollection::remove() and ::contains() wrong not clear()?

          I think i have a good grasp on the issue and try to reproduce it.

          Show
          Benjamin Eberlei added a comment - Sorry, but isn't then PersistentCollection::remove() and ::contains() wrong not clear()? I think i have a good grasp on the issue and try to reproduce it.
          Hide
          Benjamin Eberlei added a comment -

          I added a testcase tests/Doctrine/Tests/ORM/Functional/Ticket/DDC849Test.php that shows everything is working correctly. I cannot reproduce your problem.

          Can you try to extend this phpunit testcase to show the failure?

          I downgrade to trivial and detach the issue from RC1

          Show
          Benjamin Eberlei added a comment - I added a testcase tests/Doctrine/Tests/ORM/Functional/Ticket/DDC849Test.php that shows everything is working correctly. I cannot reproduce your problem. Can you try to extend this phpunit testcase to show the failure? I downgrade to trivial and detach the issue from RC1
          Hide
          Benjamin Eberlei added a comment -

          Duplicate to DDC-1189

          Show
          Benjamin Eberlei added a comment - Duplicate to DDC-1189
          Hide
          Benjamin Eberlei added a comment -

          Fixed

          Show
          Benjamin Eberlei added a comment - Fixed

            People

            • Assignee:
              Roman S. Borschel
              Reporter:
              Jáchym Toušek
            • Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: