/** * Creates a new Sale model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($object_id = 0) { $model = new Sale(); if ($object_id) { $object = Object::findOne($object_id); if ($object) { $temp = ArrayHelper::toArray($object->sale); unset($temp['id']); foreach ($temp as $k => $v) { $model->{$k} = $v; } } $model->view_ids = ArrayHelper::toArray($object->sale->view_ids); $model->facility_ids = ArrayHelper::toArray($object->sale->facility_ids); } $model->status = 1; $model->sold = 1; $model->code = rand(100000000, 999999999); for ($i = 1; $i <= Lang::find()->count(); $i++) { $model_content[$i] = new SaleLang(); $model_content[$i]['lang_id'] = $i; $model_content[$i]['id'] = 0; } if ($model->load(Yii::$app->request->post()) && Model::loadMultiple($model_content, Yii::$app->request->post()) && Model::validateMultiple($model_content) && $model->validate() && $model->district_id > 0) { $model->save(); foreach ($model_content as $key => $content) { $content->id = $model->id; $content->lang_id = $key; $content->save(false); } return $this->redirect(['update', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'model_content' => $model_content]); } }
public function actionDoc() { /* @var $model Object*/ if (($model = Object::findOne(\Yii::$app->request->post('id'))) !== null) { $files = []; foreach ($model->files as $file) { $files[] = $file->hash . '.' . $file->type; } return Json::encode(['success' => true, 'files' => $files]); } return Json::encode(['success' => false]); }
public function afterDelete() { SalePhoto::delPhotos($this->id); $path = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $this->id; BaseFileHelper::removeDirectory($path); if (!self::findOne(['object_id' => $this->object_id])) { Object::findOne($this->object_id)->delete(); } return parent::afterDelete(); }
/** * Finds the Object model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Object the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Object::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }