PHP中的数组foreach

时间:2017-09-06 09:12:17

标签: php laravel

基本上我只是想让一个阵列与我的前辈一起工作。该阵列包含来自每个设备的数据(约10000个设备),由于数据较旧,其中一些设备为空。我需要在下面的foreach中使用这个数组,以确保创建一个$ newFault,如果它有错误的软件。我超级坚持,无法让它工作。

任何线索?

请注意,我的数组必须能够使用下面的DeviceLogs关系。

     public function createViaCameras()
{
    set_time_limit(120);


    //This part removes the entire existing FirmwareFault database for the new info to go in.
    FirmwareFault::truncate();
    CameraUnlinked::truncate();

    //This part imports all cameras from today.
    $today = (new \DateTime);
    $today->modify('-3 day');
    $tomorrow = (new \DateTime);
    $tomorrow->modify('+1 day');

    $devices = Device::take(10)->get();
        foreach ($devices as $device) {


    $logs[$device->id] = DeviceLog::whereNotIn('model', ['Test', 'Test2'])->whereDeviceId($device->id)->whereBetween('created_at', [$today, $tomorrow])->orderBy('created_at', 'desc')->first();

}


    foreach ($logs as $log) {

        if (count($log->camera)) {
            if ($log->model = $log->camera->name) {
                if ($log->acaps == '1' && $log->version !== $log->camera->fwts) {
                        $newFwFault = new FirmwareFault();
                        $newFwFault->serial = $log->device->serial;
                        $newFwFault->fw = $log->version;
                        $newFwFault->model = $log->model;
                        $newFwFault->new_fw = $log->camera->fwts;
                        $newFwFault->fault_id = $log->acaps;
                        $newFwFault->save();
                    } elseif ($log->acaps == '2' && $log->version !== $log->camera->fwtb) {
                        $newFwFault = new FirmwareFault();
                        $newFwFault->serial = $log->device->serial;
                        $newFwFault->fw = $log->version;
                        $newFwFault->model = $log->model;
                        $newFwFault->new_fw = $log->camera->fwdt;
                        $newFwFault->fault_id = $log->acaps;
                        $newFwFault->save();
                    } elseif ($log->acaps == '3' && $log->version !== $log->camera->fwppt) {
                        $newFwFault = new FirmwareFault();
                        $newFwFault->serial = $log->device->serial;
                        $newFwFault->fw = $log->version;
                        $newFwFault->model = $log->model;
                        $newFwFault->new_fw = $log->camera->fwppt;
                        $newFwFault->fault_id = $log->acaps;
                        $newFwFault->save();
                    } elseif ($log->acaps == '4' && $log->version !== $log->camera->fw) {
                        $newFwFault = new FirmwareFault();
                        $newFwFault->serial = $log->device->serial;
                        $newFwFault->fw = $log->version;
                        $newFwFault->model = $log->model;
                        $newFwFault->new_fw = $log->camera->fw;
                        $newFwFault->fault_id = $log->acaps;
                        $newFwFault->save();
                    } elseif ($log->acaps == '0' && $log->version !== $log->camera->fw) {
                        $newFwFault = new FirmwareFault();
                        $newFwFault->serial = $log->device->serial;
                        $newFwFault->fw = $log->version;
                        $newFwFault->model = $log->model;
                        $newFwFault->new_fw = $log->camera->fw;
                        $newFwFault->fault_id = $log->acaps;
                        $newFwFault->save();
                    } else {
                        $newUnlink = new CameraUnlinked();
                        $newUnlink->serial = $log->device->serial;
                        $newUnlink->model = $log->model;
                        $newUnlink->save();
                    }
            } else {
            $newUnlink = new CameraUnlinked();
            $newUnlink->serial = $log->device->serial;
            $newUnlink->model = $log->model;
            $newUnlink->save();  
            }
        } else {
            $newUnlink = new CameraUnlinked();
            $newUnlink->serial = $log->device->serial;
            $newUnlink->model = $log->model;
            $newUnlink->save();  
        };

    };

    return redirect()->back();
}

1 个答案:

答案 0 :(得分:0)

//我尝试进行一些重构,但很难理解你想要实现什么,或者你的问题是什么。我稍微清理了你的代码并添加了一些注释,我希望这可以帮助你找出问题所在。我重构了你的循环,但是(没有任何方法来测试它)我相信它的实际结果和以前一样。

public function createViaCameras()
    {
        set_time_limit(120);

        //This part removes the entire existing FirmwareFault database for the new info to go in.
        FirmwareFault::truncate();
        CameraUnlinked::truncate();

        //This part imports all cameras from today.
        $threeDaysAgo = (new \DateTime)->modify('-3 day');
        $tomorrow     = (new \DateTime)->modify('+1 day');

        $devices = Device::take(10)->get();  // Why only 10? You also might want to use limit and offset instead, they are closer to the actual query language
        $logs = [];                         // Instantiate first. If the following loop doesn't find any logs, your $logs loop will fail.
        foreach ($devices as $device) {
            $logs[$device->id] = DeviceLog::whereNotIn('model', ['Test', 'Test2'])
                ->whereDeviceId($device->id)
                ->whereBetween('created_at', [$threeDaysAgo, $tomorrow]) // A log that was created tomorrow???
                ->orderBy('created_at', 'desc')
                ->first();                                               // Why only the first log?
        }

        foreach ($logs as $log) {

            if (
                !count($log->camera) ||             // $log->camera is an object. Use another function to do the check that you want. What are you checking for`?
                $log->model !== $log->camera->name
            ) {
                $model = new CameraUnlinked();
                continue;
            }

            $condition1 = $log->acaps === '1' && $log->version !== $log->camera->fwts;
            $condition2 = $log->acaps === '2' && $log->version !== $log->camera->fwtb;
            $condition3 = $log->acaps === '3' && $log->version !== $log->camera->fwppt;
            $condition4 = $log->acaps === '4' && $log->version !== $log->camera->fw;
            $condition5 = $log->acaps === '0' && $log->version !== $log->camera->fw;

            if ($condition1 || $condition2 || $condition3 || $condition4 || $condition5) {
                $model = new FirmwareFault();
                $model->fw = $log->version;
                $model->fault_id = $log->acaps;

                if ($condition1) {
                    $model->new_fw = $log->camera->fwts;
                } elseif ($condition2) {
                    $model->new_fw = $log->camera->fwdt;
                } elseif ($condition3) {
                    $model->new_fw = $log->camera->fwppt;
                } elseif ($condition4) {
                    $model->new_fw = $log->camera->fw;
                } elseif ($condition5) {
                    $model->new_fw = $log->camera->fw;
                }
            } else {
                $model = new CameraUnlinked();
            }

            $model->serial = $log->device->serial;
            $model->model = $log->model;
            $model->save();
        };

        return redirect()->back();
    }