Copying-Objects.txt 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. = Copying Persisted Objects =
  2. Propel provides the {{{copy()}}} method to perform copies of mapped row in the database. Note that Propel does '''not''' override the {{{__clone()}}} method; this allows you to create local duplicates of objects that map to the same persisted database row (should you need to do this).
  3. The {{{copy()}}} method by default performs shallow copies, meaning that any foreign key references will remain the same.
  4. {{{
  5. #!php
  6. <?php
  7. $a = new Author();
  8. $a->setFirstName("Aldous");
  9. $a->setLastName("Huxley");
  10. $p = new Publisher();
  11. $p->setName("Harper");
  12. $b = new Book();
  13. $b->setTitle("Brave New World");
  14. $b->setPublisher($p);
  15. $b->setAuthor($a);
  16. $b->save(); // so that auto-increment IDs are created
  17. $bcopy = $b->copy();
  18. var_export($bcopy->getId() == $b->getId()); // FALSE
  19. var_export($bcopy->getAuthorId() == $b->getAuthorId()); // TRUE
  20. var_export($bcopy->getAuthor() === $b->getAuthor()); // TRUE
  21. ?>
  22. }}}
  23. == Deep Copies ==
  24. By calling {{{copy()}}} with a {{{TRUE}}} parameter, Propel will create a deep copy of the object; this means that any related objects will also be copied.
  25. To continue with example from above:
  26. {{{
  27. #!php
  28. <?php
  29. $bdeep = $b->copy(true);
  30. var_export($bcopy->getId() == $b->getId()); // FALSE
  31. var_export($bcopy->getAuthorId() == $b->getAuthorId()); // FALSE
  32. var_export($bcopy->getAuthor() === $b->getAuthor()); // FALSE
  33. ?>
  34. }}}