protected function findModel($id) { if (($model = Material::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Material::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Updates an existing Supplies model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $media = Media::find()->orderBy('id DESC')->all(); $type_mat = Material::find()->all(); $type_blind = array(0 => 'Выберите тип жалюзеу', 1 => 'горизонтальные', 2 => 'рулонные', 3 => 'вертикальные'); $arr_tmat[0] = 'Выберите тип материала'; foreach ($type_mat as $v) { $arr_tmat[$v->id] = $v->name; } $color = Color::find()->all(); $arr_color[0] = 'Выберите цвет'; foreach ($color as $v) { $arr_color[$v->id] = $v->value; } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['update', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'media' => $media, 'type_mat' => $arr_tmat, 'type_blind' => $type_blind, 'color' => $arr_color]); } }
/** * @return \yii\db\ActiveQuery */ public function getMaterial() { return $this->hasOne(Material::className(), ['id' => 'material_id']); }
private function upload($post = []) { if (empty($post) || empty($_FILES['FileData'])) { throw new NotFoundHttpException(Yii::t('yii', '没有上传文件')); } //实例化上传组件 $uploader = Yii::createObject(['class' => 'common\\components\\Uploader', 'savePath' => '@upload/' . $post['type'] . '/', 'allowExts' => $this->allowTypes[$post['type']], 'maxSize' => $this->forevelMaterialSizes[$post['type']]]); //执行上传 $ret = $uploader->upload($_FILES['FileData']); if ($ret === false) { throw new NotFoundHttpException(Yii::t('yii', $uploader->errorMsg)); } //用返回的信息拼接图片路径,将图片上传到微信 $filePath = $uploader->uploadFileInfo['savepath'] . $uploader->uploadFileInfo['savename']; if (class_exists('\\CURLFile')) { $data = ['media' => new \CURLFile($filePath)]; } else { $data = ['media' => '@' . $filePath]; } $isVideo = false; $videoInfo = []; //判断是不是视频 if ($post['type'] == 'video') { $isVideo = true; $videoInfo = ['title' => $post['title'], 'introduction' => $post['introduction']]; } $uploadRet = $this->wechat->uploadForeverMedia($data, $post['type'], $isVideo, $videoInfo); if ($uploadRet == false || empty($uploadRet)) { throw new NotFoundHttpException(Yii::t('yii', '上传素材到微信失败')); } //将素材信息保存到数据库,以方便展示 $model = new Material(); $model->public_id = $this->pid; $model->name = $_FILES['FileData']['name']; //原始文件名 $model->filepath = $post['type'] . '/' . $uploader->uploadFileInfo['secondfilePath']; $model->filename = $uploader->uploadFileInfo['savename']; $model->type = $post['type']; //获取图片大小 if ($post['type'] == 'image' || $post['type'] == 'thumb') { $picSize = getimagesize($filePath); $model->imgwidth = $picSize[0]; $model->imgheight = $picSize[1]; } $model->filesize = $_FILES['FileData']['size']; $model->create_time = time(); $material_id = 0; if ($model->save()) { $material_id = $model->id; } $uploadRet['material_id'] = $material_id; return $uploadRet; }
/** * Finds the Material model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Material the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Material::find()->where(['id' => $id])->with('sites')->asArray()->one()) !== null) { return $model; } else { Yii::$app->response->format = 'json'; Yii::$app->response->setStatusCode(404); return ['message' => 'Record not found']; } }
public static function getOneAddSupplies($id, $title) { $supl = \backend\modules\supplies\models\Supplies::find()->where(['id' => $id])->one(); $html = ''; $html .= '<tr class="itemPage" page-id="' . $title . '" materials-id="' . $supl->id . '" item-type="materials">'; $html .= '<td><img src="' . $supl->images . '" style = "width:100px;"/></td>'; $html .= '<td>' . $supl->code . '</td>'; $type_materials = Material::find()->where(['id' => $supl->type_mat])->one(); $html .= '<td>' . $type_materials->name . '</td>'; if ($supl->type_blind == 1) { $html .= '<td>Горизонтальные</td>'; } if ($supl->type_blind == 2) { $html .= '<td>Вертикальные</td>'; } if ($supl->type_blind == 3) { $html .= '<td>Рулонные</td>'; } $html .= '<td>' . $supl->type_width . '</td>'; $color = Color::find()->where(['id' => $supl->color])->one(); $html .= '<td><div class="colorSuplies" style="width:50px;height:25px;background: ' . $color->value . '"></div></td>'; $html .= '<td>' . $supl->price . '</td>'; $html .= '<td><a id-materials="' . $supl->id . '"class = "delSuplies" href="#">Открепить</a></td>'; $html .= '</tr>'; return $html; }