查看ID时数据库中的条目未被覆盖

时间:2016-01-26 13:58:01

标签: php yii

我目前正在查看前一个开发人员制作的脚本,该脚本是为了查看表,如果id不存在则创建新代码,如果它存在,则覆盖现有代码。

听起来相当简单,但我无法理解Yii如何管理覆盖和新的验证码。它只是添加了新的记录,而不是写作。

  $invitingUser = User::model()->findByPk(Yii::app()->user->id);

        if ($invitingUser->isAttending($eventId)) {
            // Event attending
            $event = Event::model()->findByPk($eventId);

            // 
            // Uncomment the following line if AJAX validation is needed
            $this->performAjaxValidation(array($guestInviteForm));

            if (isset($_POST['GuestInviteForm'])) {
                $guestInviteForm->attributes = $_POST['GuestInviteForm'];

                // Perform Validation
                $valid = $guestInviteForm->validate();

                if ($valid) {
                    // Check if a Verification Code entry for this user already exists
                    $existingVerificationCode = VerificationCode::model()->findByAttributes(array('user_id' => $user->user_id, 'type' => VerificationCode::TYPE_GUEST_INVITE));


                   //THE CODE ONLY SEEMS TO RUN THIS. 
                    if (is_null($existingVerificationCode)) {
                        // Create Verification Code instance
                        $verificationCode = new VerificationCode();
                        $verificationCode->type = VerificationCode::TYPE_GUEST_INVITE;
                        $verificationCode->user_id = $invitingUser->id;
                        $verificationCode->verification_code = VerificationCode::generateVerificationCode();
                        $verificationCode->forename = $guestInviteForm->forename;
                        $verificationCode->surname = $guestInviteForm->surname;
                        $verificationCode->event_id = $eventId;

                        $verificationCode->save(false);
                    } else {
                        // Update existing Verification Code enty
                        $existingVerificationCode->type = VerificationCode::TYPE_GUEST_INVITE;
                        $existingVerificationCode->user_id = $invitingUser->id;
                        $existingVerificationCode->forename = $guestInviteForm->forename;
                        $existingVerificationCode->surname = $guestInviteForm->surname;
                        $code = $existingVerificationCode->verification_code = VerificationCode::generateVerificationCode();
                        $existingVerificationCode->save(false);
                    }

代码似乎永远不会进入其他地方

//THE CODE ONLY SEEMS TO RUN THIS. 
                        if (is_null($existingVerificationCode)) {
                            // Create Verification Code instance
                            $verificationCode = new VerificationCode();
                            $verificationCode->type = VerificationCode::TYPE_GUEST_INVITE;
                            $verificationCode->user_id = $invitingUser->id;
                            $verificationCode->verification_code = VerificationCode::generateVerificationCode();
                            $verificationCode->forename = $guestInviteForm->forename;
                            $verificationCode->surname = $guestInviteForm->surname;
                            $verificationCode->event_id = $eventId;

                            $verificationCode->save(false);
                        } else {
                            // Update existing Verification Code enty
                            $existingVerificationCode->type = VerificationCode::TYPE_GUEST_INVITE;
                            $existingVerificationCode->user_id = $invitingUser->id;
                            $existingVerificationCode->forename = $guestInviteForm->forename;
                            $existingVerificationCode->surname = $guestInviteForm->surname;
                            $code = $existingVerificationCode->verification_code = VerificationCode::generateVerificationCode();
                            $existingVerificationCode->save(false);
                        }

2 个答案:

答案 0 :(得分:0)

第一。表单验证后:

if ($valid) {

验证码选择完成:

$existingVerificationCode = VerificationCode::model()->findByAttributes(array('user_id' => $user->user_id, 'type' => VerificationCode::TYPE_GUEST_INVITE));

如果你将它翻译成SQL,它将是这样的:

SELECT * FROM verification_code WHERE user_id=x AND type=x

第二。检查记录是否存在

if (is_null($existingVerificationCode)) {

如果不是 - 创建新的,填充,保存。

其他更新:

$existingVerificationCode->type = VerificationCode::TYPE_GUEST_INVITE;
$existingVerificationCode->user_id = $invitingUser->id;
$existingVerificationCode->forename = $guestInviteForm->forename;
$existingVerificationCode->surname = $guestInviteForm->surname;
$code = $existingVerificationCode->verification_code = VerificationCode::generateVerificationCode();
$existingVerificationCode->save(false);

save(false)表示无需验证即可保存。考虑模型属性 - 数据库表中的字段。

答案 1 :(得分:0)

似乎它曾经进入ELSE,因为它永远不会找到$ existingVerificationCode。

我们没有完整的代码,但从我看到的情况来看,我怀疑您是否正在检查错误的用户,因为为$ user搜索事件是很奇怪的,如果并不存在,创建一个与$ invitingUser相关的。

$existingVerificationCode = VerificationCode::model()->findByAttributes(array('user_id' => $user->user_id, 'type' => VerificationCode::TYPE_GUEST_INVITE));
// ...
$verificationCode->user_id = $invitingUser->id;