title: sfDoctrineForm - How can i check if the object in an embedded form exists and relate it to the new parent object instead of creating a new one?
body: <p>Im working on a user registration form that involves 3 different objects, The user, member profile, and member organization. Im trying to embed all these into a single registration form like User > Member > School. The basic implementation of this works just fine with the typical <code>sfDoctrineForm::embedRealtion</code> procedure.</p>
  
  <p>The problem is the organization has a unique index on its <code>member_number</code>, the only time the the supplied value for this wont be in the database is when that user is first person to sign up from their organization. So in this case we will get a vlaidation error (or a key constraint violation if we turn the validation off).</p>
  
  <p>What I want to happen instead, is that i check for the existence of the MemberOrganization with the same <code>member_number</code> in the database (either in a pre/post validator  or in updateObject or wherever is appropriate). If it the member number already exists then i want to relate the new MemberProfile to this existing organization instead of linking it to the new one that was submitted, throwing out all the new values for that organization.</p>
  
  <p>Ive tried modifying the object in the organization form via validation but this always results in a <code>organization_id</code> constraint violation coming from the profile:</p>
  
  <pre><code>$object = $this-&gt;getObject();
  $table = $object-&gt;getTable();
  $existing = $table-&gt;findOneByMemberNumber($values['member_number']);
  if($existing)
  {
    $members = clone $object-&gt;Members;
    $object-&gt;assignIdentifier($existing-&gt;identifier());
    $object-&gt;fromArray($existing-&gt;toArray(false), false);
  
    foreach($members as $member)
    {
      $member-&gt;link($object);
    }
  
    $values = $object-&gt;toArray(false); // return only the direct values
  }
  
  return $values;
  </code></pre>
  
  <p>The schema looks something like this:</p>
  
  <pre><code>User:
    columns:
    username: {type: string(100)}
    email: {type: string(255), unique: true}
  
  MemberProfile:
    columns:
      # some none authentication related user details
      organization_id: {type: integer, notull: true}
      user_id: {type: integer, notnull: true}
    relations:
      User:
        local: user_id
        type: one
        foreign: id
        foreignType: one
      MemberOrganization:
        local: orgainization_id
        type: one
        foreign: id
        foreignType: many
        foreignAlias: Members
  
  MemberOrganization:
    columns:
      membership_number: {type: string(255), unique: true}
      # other organization data
  </code></pre>