Ticket #1434 (closed enhancement: fixed)

Opened 23 months ago

Last modified 22 months ago

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);

Attachments

Record.php.patch Download (0.5 KB) - added by thomas.s 23 months ago.
refresh a relation if it is not yet initialized

Change History

Changed 23 months ago by thomas.s

refresh a relation if it is not yet initialized

Changed 23 months ago by jwage

  • milestone set to 1.1.0

Changed 22 months ago by thomas.s

sfFormDoctrine::_construct() does not check for null anymore (that's a good thing). My suggested change in Record.php has to be adjusted to:

if (!$this->$key) {
   $this->refreshRelated($key);
}

Changed 22 months ago by jwage

  • status changed from new to closed
  • resolution set to fixed

In r5045 this was fixed.

Changed 22 months ago by jackbravo

I'm not 100% sure... but shouldn't this be made also on synchronizeWithArray()???

Note: See TracTickets for help on using tickets.