public function actionCreate($id) { if (!($category = Category::findOne($id))) { return $this->redirect(['/admin/' . $this->module->id]); } $model = new Item(); if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { $model->category_id = $category->primaryKey; if (isset($_FILES) && $this->module->settings['articleThumb']) { $model->image = UploadedFile::getInstance($model, 'image'); if ($model->image && $model->validate(['image'])) { $model->image = Image::upload($model->image, 'bookmark'); } else { $model->image = ''; } } if ($model->save()) { $this->flash('success', Yii::t('easyii/bookmark', 'Article created')); return $this->redirect(['/admin/' . $this->module->id . '/items/edit', 'id' => $model->primaryKey]); } else { $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors())); return $this->refresh(); } } } else { return $this->render('create', ['model' => $model, 'category' => $category]); } }
/** * Module database install * */ public function dbInstall() { $db = Yii::$app->db; $tableOptions = null; if ($db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=MyISAM'; } $db->createCommand()->createTable(Category::tableName(), ['category_id' => 'pk', 'title' => Schema::TYPE_STRING . '(128) NOT NULL', 'image' => Schema::TYPE_STRING . '(128) DEFAULT NULL', 'order_num' => Schema::TYPE_INTEGER, 'slug' => Schema::TYPE_STRING . '(128) DEFAULT NULL', 'tree' => Schema::TYPE_INTEGER, 'lft' => Schema::TYPE_INTEGER, 'rgt' => Schema::TYPE_INTEGER, 'depth' => Schema::TYPE_INTEGER, 'status' => Schema::TYPE_BOOLEAN . " DEFAULT '1'"], $tableOptions)->execute(); $db->createCommand()->createIndex('slug', Category::tableName(), 'slug', true)->execute(); $db->createCommand()->createTable(Item::tableName(), ['item_id' => 'pk', 'category_id' => Schema::TYPE_INTEGER, 'title' => Schema::TYPE_STRING . '(128) NOT NULL', 'image' => Schema::TYPE_STRING . '(128) DEFAULT NULL', 'url' => Schema::TYPE_STRING . '(255) DEFAULT NULL', 'time' => Schema::TYPE_INTEGER . " DEFAULT '0'", 'status' => Schema::TYPE_BOOLEAN . " DEFAULT '1'"], $tableOptions)->execute(); }
public function getCategory() { return $this->hasOne(Category::className(), ['category_id' => 'category_id']); }