- What's new in Propel 2.0
- Changelog
- API Documentation
- Installing Propel
- Building a project
- Basic CRUD
- Relationships
- Transactions
- Behaviors
- Logging And Debugging
- Inheritance
- Migrations
- Configuration
- XML Schema Format
- Active Record Classes
- Active Query Classes
- Configuration Properties Reference
- Compatibility index
- Writing A Behavior
- aggregate_column
- archivable
- auto_add_pk
- delegate
- i18n
- nested_set
- query_cache
- sluggable
- timestampable
- sortable
- validate
- versionable
- concrete_inheritance
- User-Contributed Behaviors
- Working With Propel's Test Suite
- Additional SQL Files
- Advanced Column Types
- How to Use Namespaces
- Model Introspection At Runtime
- Multi-Component Data Model
- Object Copy
- Replication
- Using Propel With MSSQL Server
- Using SQL Schemas
- Working With Existing Databases
- Working with Symfony2
- Working with Silex
Setup
Basics
Reference
Behaviors
Cookbook
Copying Persisted Objects
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).
The copy()
method by default performs shallow copies, meaning that any foreign key references will remain the same.
<?php
$a = new Author();
$a->setFirstName("Aldous");
$a->setLastName("Huxley");
$p = new Publisher();
$p->setName("Harper");
$b = new Book();
$b->setTitle("Brave New World");
$b->setPublisher($p);
$b->setAuthor($a);
$b->save(); // so that auto-increment IDs are created
$bcopy = $b->copy();
var_export($bcopy->getId() == $b->getId()); // FALSE
var_export($bcopy->getAuthorId() == $b->getAuthorId()); // TRUE
var_export($bcopy->getAuthor() == $b->getAuthor()); // TRUE
Deep Copies
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.
To continue with example from above:
<?php
$bdeep = $b->copy(true);
var_export($bdeep->getId() == $b->getId()); // FALSE
var_export($bdeep->getAuthorId() == $b->getAuthorId()); // FALSE
var_export($bdeep->getAuthor() == $b->getAuthor()); // FALSE