插入操作前如何检查某些条件

时间:2017-01-06 13:32:31

标签: php mysql

我有两个返回数组输出的变量,这是示例。

$ scheduleRrecords:

Array
(
    [0] => stdClass Object
        (
            [scheduleId] => 1
            [ownerId] => 32
            [userId] => 33
            [scheduleCategoryId] => 53
            [shiftTimeId] => 8
            [dayPartId] => 34
            [shiftBreakId] => 3
            [colorCodeId] => 1
            [scheduleDate] => 2017-01-02
            [startTime] => 20:00:00
            [endTime] => 22:00:00
            [shiftNote] => Sidecar Swing
            [scheduleNote] => 
            [alert] => 0
            [scheduleDay] => Monday
        )

    [1] => stdClass Object
        (
            [scheduleId] => 2
            [ownerId] => 32
            [userId] => 33
            [scheduleCategoryId] => 53
            [shiftTimeId] => 7
            [dayPartId] => 33
            [shiftBreakId] => 2
            [colorCodeId] => 2
            [scheduleDate] => 2017-01-02
            [startTime] => 10:00:00
            [endTime] => 13:00:00
            [shiftNote] => Mid
            [scheduleNote] => 
            [alert] => 0
            [scheduleDay] => Monday
        )

    [2] => stdClass Object
        (
            [scheduleId] => 3
            [ownerId] => 32
            [userId] => 33
            [scheduleCategoryId] => 53
            [shiftTimeId] => 8
            [dayPartId] => 34
            [shiftBreakId] => 3
            [colorCodeId] => 1
            [scheduleDate] => 2017-01-03
            [startTime] => 20:00:00
            [endTime] => 22:00:00
            [shiftNote] => Sidecar Swing
            [scheduleNote] => 
            [alert] => 0
            [scheduleDay] => Tuesday
        )

    [3] => stdClass Object
        (
            [scheduleId] => 4
            [ownerId] => 32
            [userId] => 33
            [scheduleCategoryId] => 53
            [shiftTimeId] => 7
            [dayPartId] => 33
            [shiftBreakId] => 3
            [colorCodeId] => 2
            [scheduleDate] => 2017-01-03
            [startTime] => 10:00:00
            [endTime] => 13:00:00
            [shiftNote] => Mid
            [scheduleNote] => 
            [alert] => 0
            [scheduleDay] => Tuesday
        )

)

$ checkRequestOffObj:

Array
(
    [0] => stdClass Object
        (
            [requestOffId] => 4
            [ownerId] => 32
            [userId] => 33
            [requestOffDate] => 2017-01-09
            [dayPartId] => 33
            [isPermRequest] => no
            [requestOffNote] => 
            [permNote] => 
            [requestStatus] => pending
            [approvedOrDeclinedBy] => 
            [approvedOrDeclinedNote] => 
            [firstName] => Erick
            [lastName] => Venere
            [email] => erick.portalic@gmail.com
            [dayPartName] => Lunch
            [status] => 1
        )

    [1] => stdClass Object
        (
            [requestOffId] => 5
            [ownerId] => 32
            [userId] => 33
            [requestOffDate] => 2017-01-10
            [dayPartId] => 33
            [isPermRequest] => yes
            [requestOffNote] => 
            [permNote] => 
            [requestStatus] => pending
            [approvedOrDeclinedBy] => 
            [approvedOrDeclinedNote] => 
            [firstName] => Erick
            [lastName] => Venere
            [email] => erick.portalic@gmail.com
            [dayPartName] => Lunch
            [status] => 1
        )

)

现在我不想在这种情况下将记录插入数据库:

  1. 如果[isPermRequest] => no,则不要将记录插入[requestOffDate] => 2017-01-09日期。
  2. 如果[isPermRequest] => yes,请检查[requestOffDate] => 2017-01-09天。我们假设2017-01-09Monday,因此请勿向所有Monday插入记录。
  3. 我有一个主for loop,用于$scheduleRrecords循环数据并在此循环中检查isPermRequest。

    这是我的核心逻辑:

    if (!empty($scheduleRrecords)) {
        for ($a=0; $a<count($scheduleRrecords); $a++) {
            if (isset($scheduleRrecords[$a]->ownerId) && !empty($scheduleRrecords[$a]->ownerId)) {
                $aOwnerId = $scheduleRrecords[$a]->ownerId;
            } else {
                $aOwnerId = "";
            }
    
            if (isset($scheduleRrecords[$a]->userId) && !empty($scheduleRrecords[$a]->userId)) {
                $aUserId = $scheduleRrecords[$a]->userId;
            } else {
                $aUserId = "";
            }
    
            if (isset($scheduleRrecords[$a]->scheduleCategoryId) && !empty($scheduleRrecords[$a]->scheduleCategoryId)) {
                $aScheduleCategoryId = $scheduleRrecords[$a]->scheduleCategoryId;
            } else {
                $aScheduleCategoryId = "";
            }
    
            if (isset($scheduleRrecords[$a]->shiftTimeId) && !empty($scheduleRrecords[$a]->shiftTimeId)) {
                $aShiftTimeId = $scheduleRrecords[$a]->shiftTimeId;
            } else {
                $aShiftTimeId = "";
            }
    
            if (isset($scheduleRrecords[$a]->dayPartId) && !empty($scheduleRrecords[$a]->dayPartId)) {
                $aDayPartId = $scheduleRrecords[$a]->dayPartId;
            } else {
                $aDayPartId = "";
            }
    
            if (isset($scheduleRrecords[$a]->shiftBreakId) && !empty($scheduleRrecords[$a]->shiftBreakId)) {
                $aShiftBreakId = $scheduleRrecords[$a]->shiftBreakId;
            } else {
                $aShiftBreakId = "";
            }
    
            if (isset($scheduleRrecords[$a]->colorCodeId) && !empty($scheduleRrecords[$a]->colorCodeId)) {
                $aColorCodeId = $scheduleRrecords[$a]->colorCodeId;
            } else {
                $aColorCodeId = "";
            }
    
            if (isset($scheduleRrecords[$a]->scheduleDate) && !empty($scheduleRrecords[$a]->scheduleDate)) {
                $aScheduleDate = $scheduleRrecords[$a]->scheduleDate;
            } else {
                $aScheduleDate = "";
            }
    
            if (isset($scheduleRrecords[$a]->startTime) && !empty($scheduleRrecords[$a]->startTime)) {
                $aStartTime = $scheduleRrecords[$a]->startTime;
            } else {
                $aStartTime = "";
            }
    
            if (isset($scheduleRrecords[$a]->endTime) && !empty($scheduleRrecords[$a]->endTime)) {
                $aEndTime = $scheduleRrecords[$a]->endTime;
            } else {
                $aEndTime = "";
            }
    
            if (isset($scheduleRrecords[$a]->shiftNote) && !empty($scheduleRrecords[$a]->shiftNote)) {
                $aShiftNote = $scheduleRrecords[$a]->shiftNote;
            } else {
                $aShiftNote = "";
            }
    
            if (isset($scheduleRrecords[$a]->scheduleNote) && !empty($scheduleRrecords[$a]->scheduleNote)) {
                $aScheduleNote = $scheduleRrecords[$a]->scheduleNote;
            } else {
                $aScheduleNote = "";
            }
    
            // Get next week date from scheduleDate
            $getUserNextWeekArr = GlobalHelper::getUserNextWeek($aOwnerId, $aScheduleDate);
            $nextDate = $getUserNextWeekArr['next'];
    
            // Get weekly date array from selected start date
            $getOwnerWeekListArr = GlobalHelper::getOwnerWeekList($aOwnerId, $copyStartDate);
    
            // Get scheduleDate day name
            $dayOfWeek = date('D', strtotime($aScheduleDate));
    
            // Obtain The Key Of The Array
            $nextDate = "";
            foreach ($getOwnerWeekListArr as $key => $val) {
                if ($val['day'] == $dayOfWeek) {
                    $nextDate .= $getOwnerWeekListArr[$key]['date'];
                }
            }
    
            $scheduleDate = str_replace('/', '-', date('Y-m-d', strtotime($nextDate)));
    
            $checkRequestOffObj = GlobalHelper::checkRequestOff($aOwnerId, $aUserId, $aDayPartId, $scheduleDate);
        }
    }
    

    这是我的登录信息,但它无效:

    if (!empty($checkRequestOffObj)) {
        for ($t=0; $t<count($checkRequestOffObj); $t++) {
            $tRequestOffDate = $checkRequestOffObj[$t]->requestOffDate;
            $tRequestOffDay = date('D', strtotime($checkRequestOffObj[$t]->requestOffDate));
            $tDayPartId = $checkRequestOffObj[$t]->dayPartId;
    
            if ($checkRequestOffObj[$t]->isPermRequest == 'yes') {
                if ($dayOfWeek == $tRequestOffDay && $aDayPartId != $tDayPartId) {
                    $result = DB::table('schedules')
                        ->where('ownerId', '=', $aOwnerId)
                        ->where('userId', '=', $aUserId)
                        ->where('scheduleCategoryId', '=', $aScheduleCategoryId)
                        ->where('shiftTimeId', '=', $aShiftTimeId)
                        ->where('dayPartId', '=', $aDayPartId)
                        ->where('shiftBreakId', '=', $aShiftBreakId)
                        ->where('colorCodeId', '=', $aColorCodeId)
                        ->where('scheduleDate', '=', $scheduleDate)
                        ->exists();
    
                    if ($result == ""):
                        DB::table('schedules')->insert(
                            array(
                                'ownerId' => $aOwnerId,
                                'userId' => $aUserId,
                                'scheduleCategoryId' => $aScheduleCategoryId,
                                'shiftTimeId' => $aShiftTimeId,
                                'dayPartId' => $aDayPartId,
                                'shiftBreakId' => $aShiftBreakId,
                                'colorCodeId' => $aColorCodeId,
                                'scheduleDate' => $scheduleDate,
                                'startTime' => $aStartTime,
                                'endTime' => $aEndTime,
                                'shiftNote' => $aShiftNote,
                                'scheduleNote' => $aScheduleNote,
                                'createdBy' => Auth::user()->userId,
                                'isPosted' => "0",
                                'sendSchedule' => "0",
                                'cronStatus' => "0",
                            )
                        );
                    endif;
                } else {
                    continue;
                }
            } else {
                if ($aDayPartId != $tDayPartId && $scheduleDate == $tRequestOffDate) {
                    $result = DB::table('schedules')
                        ->where('ownerId', '=', $aOwnerId)
                        ->where('userId', '=', $aUserId)
                        ->where('scheduleCategoryId', '=', $aScheduleCategoryId)
                        ->where('shiftTimeId', '=', $aShiftTimeId)
                        ->where('dayPartId', '=', $aDayPartId)
                        ->where('shiftBreakId', '=', $aShiftBreakId)
                        ->where('colorCodeId', '=', $aColorCodeId)
                        ->where('scheduleDate', '=', $scheduleDate)
                        ->exists();
    
                    if ($result == ""):
                        DB::table('schedules')->insert(
                            array(
                                'ownerId' => $aOwnerId,
                                'userId' => $aUserId,
                                'scheduleCategoryId' => $aScheduleCategoryId,
                                'shiftTimeId' => $aShiftTimeId,
                                'dayPartId' => $aDayPartId,
                                'shiftBreakId' => $aShiftBreakId,
                                'colorCodeId' => $aColorCodeId,
                                'scheduleDate' => $scheduleDate,
                                'startTime' => $aStartTime,
                                'endTime' => $aEndTime,
                                'shiftNote' => $aShiftNote,
                                'scheduleNote' => $aScheduleNote,
                                'createdBy' => Auth::user()->userId,
                                'isPosted' => "0",
                                'sendSchedule' => "0",
                                'cronStatus' => "0",
                            )
                        );
                    endif;
                } else {
                    continue;
                }
            }
        }
    } else {
    
    }
    

    任何想法如何在插入操作之前检查?

    感谢。

0 个答案:

没有答案