duannuo4620 2015-10-14 22:00
浏览 32
已采纳

Yii:模型 - >保存 - 不可预测的保存

I have some view for every objects of array ($this->receiveList)and there are 2 buttons(2 forms with input submit) for every objects too. If i trying to change status value of someone of objects(models) - only one(1st) object will be changed(saved new value for status) by all 4 buttons. BUT ONLY 2 BUTTONS READY FOR THAT AND 2 for another object.
WHY ITS HAPPENING Guys?
Example img: http://s017.radikal.ru/i441/1510/c7/974e00e3f8be.jpg

$this->receiveList = Invite::model()->findAll( $criteria2 );    // array of 2 objects

for ( $key=0; $key <= count($this->receiveList) - 1; $key++ ) {
    // yes
    if ( isset($_POST['formInviteYes']['sendRequest']) ) {
        unset($_POST['formInviteYes']['sendRequest']);

        $this->receiveList[$key]->status = 1;
        $this->receiveList[$key]->save(false);

        $this->refresh();
    }
    // no
    if ( isset($_POST['formInviteNo']['sendRequest']) ) {
        unset($_POST['formInviteNo']['sendRequest']);

        $this->receiveList[$key]->status = 0;
        $this->receiveList[$key]->save(false);


        $this->refresh();
    }

    print('<form action="" method="post" id="">');
        print('<input type="submit" name="formInviteYes[sendRequest]" value="Согласиться"" class="inviteAnswer">');
    print('</form>');

    print('<form action="" method="post" id="">');
        print('<input type="submit" name="formInviteNo[sendRequest]" value="Отказаться" class="inviteAnswer">');
    print('</form>');
}
  • 写回答

2条回答 默认 最新

  • dongyi1015 2015-10-15 19:15
    关注

    You need to name submit buttons to contain info about object it's related to

            print('<input type="submit" name="formInviteNo[' . $invite->id . '][sendRequest]" value="Отказаться" class="inviteAnswer">');
    

    And then you could implement your logic in this way:

    $this->receiveList = Invite::model()->findAll( $criteria2 );    // array of 2 objects
    
    foreach ($this->receiveList as $invite) {
        // yes
        if ( isset($_POST['formInviteYes'][$invite->id]['sendRequest']) ) {
            unset($_POST['formInviteYes'][$invite->id]['sendRequest']);
    
            $invite->status = 1;
            $invite->save(false);
    
            $this->refresh();
        }
        // no
        if ( isset($_POST['formInviteNo'][$invite->id]['sendRequest']) ) {
            unset($_POST['formInviteNo'][$invite->id]['sendRequest']);
    
            $invite->status = 0;
            $invite->save(false);
    
            $this->refresh();
        }
    
    print('<form action="" method="post" id="">');
        print('<input type="submit" name="formInviteYes[' . $invite->id . '][sendRequest]" value="Согласиться"" class="inviteAnswer">');
    print('</form>');
    
    print('<form action="" method="post" id="">');
        print('<input type="submit" name="formInviteNo[' . $invite->id .'][sendRequest]" value="Отказаться" class="inviteAnswer">');
    print('</form>');
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测