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

时间:2015-10-14 22:00:01

标签: php yii

我对数组的每个对象都有一些视图($this->receiveList),并且每个对象也有2个按钮(2个带有输入提交的表单)。 如果我试图更改某个对象(模型)的状态值 - 只有一个(第一个)对象将被所有4个按钮更改(保存状态的新值)。但是只有2个按钮准备好了,2个用于另一个物体 为什么会发生这种情况?
示例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 个答案:

答案 0 :(得分:1)

您打印相关的表单白名操作和相同的ID

尝试为每个表单分配适当的操作,并为正确的ID

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

print('<form action="action2.php" method="post" id="2">');
    print('<input type="submit" name="formInviteNo[sendRequest]" value="Отказаться" class="inviteAnswer">');
print('</form>');

答案 1 :(得分:1)

您需要命名提交按钮以包含与

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

然后你可以用这种方式实现你的逻辑:

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

}