在文件上载操作期间,在yii2中保存数据库中的数据

时间:2015-11-22 23:07:48

标签: php yii yii2

我有一个上传控制器,我也在执行将其他数据保存到数据库。上传到文件夹的文件是可以的,但保存表中的其他详细信息不会发生

controller code

$images = $_FILES['evidence'];
$success = null;

$paths= ['uploads'];

// get file names
$filenames = $images['name'];


// loop and process files
for($i=0; $i < count($filenames); $i++){
//$ext = explode('.', basename($filenames[$i]));
$target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
if(move_uploaded_file($images['name'], $target)) {
    $success = true;
    $paths[] = $target;
} else {
    $success = false;

    break;
}

echo $success;
}
// check and process based on successful status 
if ($success === true) {
        $evidence = new Evidence();
        $evidence->case_ref=$id;
        $evidence->saved_on=date("Y-m-d");
        $evidence->save();

$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];

foreach ($paths as $file) {
    unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}

//  return a json encoded response for plugin to process successfully
echo json_encode($output);

执行var_dump($evidence->save())后,我收到布尔false

的错误

1 个答案:

答案 0 :(得分:1)

您可能会遇到验证错误。检查$errors属性

// check and process based on successful status 
if ($success === true) {
        $evidence = new Evidence();
        $evidence->case_ref=$id;
        $evidence->saved_on=date("Y-m-d");
        $retSave = $evidence->save();

        if($retSave == false)
        {
              var_dump($evidence->errors);
        }

$output = [];
}
....