Migrating Schemas ================= Even though MongoDB is schemaless, introducing some kind of object mapper means that the definition of your objects become your schema. You may have a situation where you rename a property in your object and you need to also load the values from older documents where the field is still using the old name. Doctrine offers a few different methods for dealing with this problem! .. note:: Features in this chapter inspired by Objectify All of the features documented in this chapter were inspired by Objectify which is an object mapper for the AppEngine datastore. You can read more about the project on the `Objectify Wiki `_. Renaming a Field ---------------- Lets say you have a document that starts off looking like this: .. code-block:: php firstName = $e[0]; $this->lastName = $e[1]; } } Moving Fields ------------- Migrating your schema can be a difficult task and Doctrine gives you a few different methods for dealing with this: - **@AlsoLoad** - load values from old fields names or transform some data using methods. - **@NotSaved** - load values into fields without saving them again. - **@PostLoad** - execute code after all fields have been loaded. - **@PrePersist** - execute code before your document gets saved. Imagine you have some address fields on a Person document: .. code-block:: php street = $street; $this->city = $city; } } /** @Document */ class Person { /** @Id */ public $id; /** @String */ public $name; /** @NotSaved */ public $street; /** @NotSaved */ public $city; /** @EmbedOne(targetDocument="Address") */ public $address; /** @PostLoad */ public function postLoad() { if ($this->street !== null || $this->city !== null) { $this->address = new Address($this->street, $this->city); } } } You can also change the data on save if that works better for you: .. code-block:: php street !== null || $this->city !== null) { $this->address = new Address($this->street, $this->city); } } }