Body: <p>I was trying to be down with the cool kids and use DB_Object models for a looped database update. I've found somethning I thought should work, but doesn't so I must be doing/understanding something wrong.</p>
  
  
  
  <pre><code>$bao = new CRM_Some_BAO();
  $bao-&gt;hair = 'long';
  $bao-&gt;find();
  while ($bao-&gt;fetch()) {
    $bao-&gt;message = 'cool hair, ' . $bao-&gt;name;
    $bao-&gt;save();
  }
  </code></pre>
  
  <p>I'd thought that something like the above would find everyone with long hair and set a message to 'cool hair, <code>&lt;name&gt;</code>'</p>
  
  <p>But actually what happens is that only the first match gets processed. I've also tried <code>clone($bao)</code> and updating the clone, but it's still same.</p>
  
  <p>What's the <a href="https://pear.php.net/manual/en/package.database.db-dataobject.db-dataobject.update.php" rel="nofollow noreferrer">DB_Object</a> way of achieving such a pattern?</p>
  
  <p>I'm currently just resorting to standard SQL.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-16T15:57:57.157Z
Comments.Text: Since you know they already exist I might try it with update() instead of save(). And while I've never tried using DB_DATAOBJECT_WHEREADD_ONLY, it seems like you might be able to omit the loop entirely and pass that param to update to be more efficient, since then it says it will do a batch update to make them all "cool" at once.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-16T16:09:35.757Z
Comments.Text: @Demerit thanks. In trying to keep the pseudo code short I should have been clear that each update is actually unique.
Comments.UserDisplayName: artfulrobot
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-16T16:13:27.777Z
Comments.Text: Ok got it. Update() alone might still work?
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-16T16:19:19.893Z
Comments.Text: @Demerit You're right, `update()` works, but CiviCRM's `save()` method does more, it also runs hooks etc. I'm looking for a generic, CiviCRM-way to do such jobs.
Comments.UserDisplayName: artfulrobot
Tags: <database><developer>
Title: How to loop and update BAO objects
ThreadText: # How to loop and update BAO objects
  
  (Asked by: username_0 on 2019-10-16)
  
  I was trying to be down with the cool kids and use DB\_Object models for a looped database update. I've found somethning I thought should work, but doesn't so I must be doing/understanding something wrong.
  
  ```
  $bao = new CRM_Some_BAO();
  $bao->hair = 'long';
  $bao->find();
  while ($bao->fetch()) {
    $bao->message = 'cool hair, ' . $bao->name;
    $bao->save();
  }
  
  ```
  
  I'd thought that something like the above would find everyone with long hair and set a message to 'cool hair, `<name>`'
  
  But actually what happens is that only the first match gets processed. I've also tried `clone($bao)` and updating the clone, but it's still same.
  
  What's the [DB\_Object](https://pear.php.net/manual/en/package.database.db-dataobject.db-dataobject.update.php) way of achieving such a pattern?
  
  I'm currently just resorting to standard SQL.
  
  **Tags:** database, developer
  
  > **Comments:**
  > **username_1**: Since you know they already exist I might try it with update() instead of save(). And while I've never tried using DB_DATAOBJECT_WHEREADD_ONLY, it seems like you might be able to omit the loop entirely and pass that param to update to be more efficient, since then it says it will do a batch update to make them all "cool" at once.  
  > **username_0**: @username_1 thanks. In trying to keep the pseudo code short I should have been clear that each update is actually unique.  
  > **username_1**: Ok got it. Update() alone might still work?  
  > **username_0**: @username_1 You're right, `update()` works, but CiviCRM's `save()` method does more, it also runs hooks etc. I'm looking for a generic, CiviCRM-way to do such jobs.  
  
  ---
  
  ## 0 answers
  
  