yii2$this->setattributes() 사용 방법

6446 단어 질문
나는 사진을 업로드할 때 만난 이 방법을 두 번 켰다.사진이 올라온 후 데이터는 모두 얻었지만 입고되지 않았다.
iii2에서 $this->setattributes($data)
$data rules , 。

사진 업로드 코드:
$model = new Device();
$post = Yii::$app->request->post();
if ($post) {
    $post['image'] = "";
    $fileObj = UploadedFile::getInstanceByName('imgFile');
    //var_dump($fileObj);exit;
    if ($fileObj) {
        // 
        $imgFilePath = "img/" . uniqid() . '.' . $fileObj->extension;
        //var_dump($imgFilePath);exit;
        if ($fileObj->saveAs($imgFilePath)) {
            $post['image'] = $imgFilePath;
        }
    }
    $insert = $model->insertData($post);

모델 코드:
public function insertData($data)
    {
// , ,$data 

$time
=
date
(
'Y-m-d H:i:s'
,
time
());
$data
[
'status'
] =
1
;
$data
[
'created_time'
] =
$time
;
$data
[
'updated_time'
] =
$time
;
if
(
isset
(
$data
[
'_csrf'
])){
unset
(
$data
[
'_csrf'
]);
}
$this
->setAttributes(
$data
);
$result
=
$this
->save();
//$result = Yii::$app->db->createCommand()->insert('device', $data)->execute();
if
(
$result
) {
return
$this
->
attributes
[
'id'
];
}
return false
;
}

좋은 웹페이지 즐겨찾기