Symfony 2 How to keep cloned entity properties unchanged on change of main entity.

Often while we clone an entity, we find the property of new cloned entity gets changed based on changes to the main entity. Mostly when the change occurs on $form->handleRequest($request);

Below magic function restricts the properties to keep their value unchanged by cloning deeper state of relationships.

Code Example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
/**
 * clone
 */
public function __clone()
{
    if ($this->id) {
        // Remove orm reference to DB.
        $this->id = null;
        // Collect books to clone so that any further change to books in
        // main entity won't affect the cloned entity.
        $this->books = clone $this->books;
    }
}
?>

Comments

Unknown said…
Thank you. Your filter explanation helped me.

Popular posts from this blog

Filtering and Sorting a Doctrine Arraycollection

Doctrine mapping (ORM) with Database (DAL), Unidirectional vs Bidirectional.

Drupal 7 - How to make dynamic select box through Drupal #ajax framework