Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally I would do something like this:</p> <pre><code>$location = new Location(); $location-&gt;name = 'yyy'; $location-&gt;save(); // this will assign location id using autoincrement // or alternatively if you have generated table classes // $location = LocationTable::getInstance()-&gt;create(array('name' =&gt; 'yyy'); // or if you have an already existing location // $location = LocationTable::getInstance()-&gt;find($location_id); // keep in mind that if you haven't generated any table class you should replace // LocationTable::getInstance() with Doctrine_Core::getTable('Location'); $user = new User(); $user-&gt;name = 'xxx'; $user-&gt;location = $location; // or $user-&gt;Location = $location; // the case of the l depends on how you have declared the model relationships $user-&gt;save; $user2 = new User(); $user2-&gt;name = 'zzz'; $user2-&gt;location = $location; $user2-&gt;save; </code></pre> <p>Generally speaking Doctrine has a lot of convenient methods to deal with relations, the correct one to use depends on your exact needs. For example you should specify how your location object is built, how many user instances do you have in the same code, if you have a location id or location data and so on.</p> <p>For point 1,2 and 3 in rails I'd use find_or_create_by method, which is not available in Doctrine, but you can always write it by yourself. So if you have the <code>LocationTable</code> class you can do this:</p> <pre><code>// in LocationTable class public function findOrCreateBy($fieldName, $value, array $data = array()) { if (!$record = $this-&gt;findBy($fieldName, $value)) { // record doesn't exist, create it with provided data $record = $this-&gt;create(array($fieldName =&gt; $value)); } // update record data $record-&gt;fromArray($data); // optionally save the record, depend on your needs $record-&gt;save(); // it won't trigger actual save if record fields aren't updated return $record; } // then in your example code you could fetch the location code with $location = LocationTable::getInstance() -&gt;findOrCreateBy('name', 'yyyy', array('field_to_update' =&gt; 'new value')); </code></pre> <p>Don't use preSave hooks for this kind of things, I think they should be used for other use cases.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload