1

This code is only updating one row (the one that matches the first in the array):

Edit: showing more code!

Here's my view. It's grabbing db entries based on an association (there's one model "clients" that has a one-to-many relationship with two other models, Programs and Users).

<div class="view">
    <h2><?php echo h($program['Program']['title'])?></h1>
    <?php echo $this->Form->create('User', array('action' => 'pushing')); ?>
    <table id="pushTable">
        <tr><th colspan="5">Select the athletes you would like to push this program to:</th></tr>
        <tr>
        <?php $i=0; ?>
        <?php foreach ($clients['Players'] as $player) {?>
            <?php if ($i == 0) {?><tr><?php } ?>
            <td><?php echo $this->Form->checkbox($player['id']) . ' ' . $player['username'];?></td>
            <?php if ($i == 4) {?></tr><?php $i = 0; } else { $i++; } ?>
        <?php } ?>
        </tr>
    </table>
    <?php echo $this->Form->end(__('Push')); ?>
</div>

In my Users controller:

public function pushing() {
    if ($this->request->is('post') || $this->request->is('put')) {
        $athletes = $this->request->data['User'];
        foreach ( $athletes as $player => $flag){
            if ($flag == 0){
                unset($athletes[$player]);
            }
        }
        $this->User->updateAll(
            array('User.data' => "'foo'"),
            array('User.id' => $athletes)
        );
        $this->Session->setFlash(__('Programs were pushed!'));
        }
    }
}

$athletes is the array is collected from checked boxes, and there seem to me no problems with that... so I'm not sure why the updateAll isn't iterating over each ID in the array...

Maybe it's not working for a db related reason? I'm developing on MAMP... maybe the db isn't set up for "atomic" stuff (only heard about that here today!).

I had previously tried using a foreach to loop over the id's, then in the foreach just did this (edit: more code!)

public function pushing() {
    if ($this->request->is('post') || $this->request->is('put')) {
        foreach ($this->request->data['User'] as $player => $flag) {
            if ($flag) {
                $this->User->id = $player; // set ID to player we want to save ($player is id that was suplpied in view to checkbox
                $this->User->saveField('data', 'foo'); // then, push the data!
            }
        }
        $this->Session->setFlash(__('Programs were pushed!'));
    }
    $this->autoRender = false;
    $this->redirect(array('action' => 'index'));                    
}

But that caused weird results with redirection. No matter where I put redirection in that action, the save just wanted to do its own thing. That $this->autoRender did nothing. The controller still tried to resolve to the /pushing/ route

2
  • It should work as is by creating an IN() statement. Did you check your SQL log? Also would be good to see more of the code if you're having redirect issues. Commented Mar 29, 2012 at 15:50
  • 1
    both should work fine (the first one is faster, as it is only a single atomic query). your issue might be somewhere else. Commented Mar 29, 2012 at 16:05

2 Answers 2

2

Give this a shot :)

In your view:

<div class="view">
        <h2><?php echo h($program['Program']['title'])?></h1>
        <?php echo $this->Form->create('User', array('action' => 'pushing')); ?>
        <table id="pushTable">
            <tr><th colspan="5">Select the athletes you would like to push this program to:</th></tr>
            <tr>
            <?php $i=0; ?>
            <?php foreach ($clients['Players'] as $player) {?>
                <?php if ($i == 0) {?><tr><?php } ?>
                <td><?php echo $this->Form->input('Player.' . $player['id']. '.id', array('type' => 'checkbox', 'value' => $player['id'])) . ' ' . $player['username'];?></td>
                <?php if ($i == 4) {?></tr><?php $i = 0; } else { $i++; } ?>
            <?php } ?>
            </tr>
        </table>
        <?php echo $this->Form->end(__('Push')); ?>
    </div>

In your controller:

public function pushing() {
    if ($this->request->is('post') || $this->request->is('put')) {
        $athletes = $this->request->data['Player'];
        $athlete_ids = array();
        foreach($athletes as $a){
            $athlete_ids[$a['id']] = $a['id'];
        }

        $this->User->updateAll(
            array('User.data' => "'foo'"),
            array('User.id' => $athlete_ids)
        );
        $this->Session->setFlash(__('Programs were pushed!'));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks William! I'm still new to Cake... why does this method work where mine didn't?
0

Hi had to change the code a bit to make it for work me..

    $contactsids = array();
    foreach($selected as $key => $val){
      if(($val <> '') && ($val <> 0) ) {                  
          $contactsids[$val] = $val;
      }                        
    } 

$this->Contact->updateAll(
  array('Contact.contact_category_id' => $category_id),
  array('Contact.id' => $contactsids)
);   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.