Ticket #1434 (closed enhancement: fixed)
Record::fromArray(array, deep), refresh relation if related record (array[key]) is transient
| Reported by: | thomas.s | Owned by: | romanb |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.1.0-ALPHA1 |
| Component: | Relations | Version: | 1.0.0 |
| Severity: | Keywords: | refresh relation fromarray sfformdoctrine form symfony | |
| Cc: | Has Test: | no | |
| Status: | Pending Core Response | Has Patch: | yes |
Description
I use symfony 1.1 and the symfony form framework. I have a relation "Profile has one Address" and two forms, ProfileForm? and AddressForm?. I embed the AddressForm? in the ProfileForm? ($this->getObject() is the Profile):
protected function embedAddressForm()
{
if (null === $this->getObject()->Address) {
$this->getObject()->Address = new Address;
}
$this->embedForm('Address', new AddressForm($this->getObject()->Address));
}
This explicit assignment of a new Address if there is not yet an Address related to the Profile in the ProfileForm? is dispensible, when we make a minor change to the class Record (see attached patch). To embed the form we could then use:
protected function embedAddressForm()
{
$this->embedForm('Address', new AddressForm($this->getObject()->Address));
}
Because the class sfFormDoctrine creates a new Address automatically if there is no related Address, we just have to check for a null-relation in the class Record and refresh that particular relation before creating the Profile from the array.
sfFormDoctrine::_construct():
$class = $this->getModelName();
if (is_null($object))
{
$this->object = new $class();
}
My suggestion for Record.php:
Index: Record.php
===================================================================
--- Record.php (revision 4876)
+++ Record.php (working copy)
@@ -1458,6 +1458,9 @@
}
if ($deep && $this->getTable()->hasRelation($key)) {
+ if (null === $this->$key) {
+ $this->refreshRelated($key);
+ }
$this->$key->fromArray($value, $deep);
} else if ($this->getTable()->hasField($key)) {
$this->set($key, $value);
