#1434: Record::fromArray(array, deep), refresh relation if related record
(array[key]) is transient
--------------------------------------------------------------------+-------
 Reporter:  thomas.s                                                |       
Owner:  romanb
     Type:  enhancement                                             |      
Status:  new   
 Priority:  minor                                                   |   
Milestone:        
Component:  Relations                                               |     
Version:  1.0   
 Keywords:  refresh relation fromarray sfformdoctrine form symfony  |    
Has_test:  0     
 Mystatus:  Pending Core Response                                   |   
Has_patch:  1     
--------------------------------------------------------------------+-------
 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);
 }}}

-- 
Ticket URL: <http://trac.doctrine-project.org/ticket/1434>
Doctrine <http://www.phpdoctrine.org>
PHP Doctrine Object Relational Mapper
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"doctrine-svn" group.
 To post to this group, send email to [email protected]
 To unsubscribe from this group, send email to [EMAIL PROTECTED]
 For more options, visit this group at 
http://groups.google.co.uk/group/doctrine-svn?hl=en-GB
-~----------~----~----~----~------~----~------~--~---

Reply via email to