2012-03-29 38 views
1

此代码仅更新一行(与阵列中第一个匹配的那一行):使用ID数组更新CakePHP中的指定行?

编辑:显示更多代码!

这是我的看法。它是基于关联抓分贝项(有一个模型“客户”具有与其他两款车型,程序和用户一个一对多的关系)。

<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> 

在我的用户控制:

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!')); 
     } 
    } 
} 

$运动员在阵列从检查盒收集起来,似乎有对我没有任何问题与...所以我不知道为什么updateAll不遍历数组中的每个ID ...

也许它不工作的分贝相关原因是什么?我正在开发MAMP ...也许数据库没有设置为“原子”的东西(今天才听说这个!)。

使用foreach循环过的ID我以前试过,然后在foreach只是做这个(编辑:更多的代码!)

​​

但导致与重定向奇怪的结果。无论我在哪里做出重定向,拯救只是想做自己的事情。 $ this-> autoRender什么也没做。该负责人还试图解析为/推/路由

+0

它应该作为是通过创建一个'IN()'声明。你有没有检查你的SQL日志?如果您遇到重定向问题,也可以查看更多代码。 – jeremyharris 2012-03-29 15:50:54

+1

都应该工作正常(第一个是速度更快,因为它只是一个单原子查询)。你的问题可能在别的地方。 – mark 2012-03-29 16:05:50

回答

2

给这一个镜头:)

在你看来:

<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> 

在你的控制器:

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!')); 
     } 
    } 
} 
+0

谢谢威廉!我还是新来的蛋糕......为什么这种方法在我的地方没有? – 2012-03-29 18:16:32

0

嗨了改变一下代码,使之成为工作的我..

$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) 
);