0
저는 뉴스 레터 응용 프로그램을 만드는 작업을 처음 접했습니다. 이를 위해 yii2.0 고급 템플릿을 사용하고 있습니다. 나는 gii CRUD에 의해 컨트롤러 모델과 폼을 생성했다. 내 컨트롤러에서 actionCreate()로 이동하는이 양식을 제출할 때 이미지 업로드로 양식 (_form.php)을 작성했는데 if ($ model-> load (Yii :: $ app-> request-> post()) & & $ model-> save()) {actionCreate()에 있습니다. 양식 데이터를 데이터베이스에 저장해야합니다. 나는 구글을 많이 가지고 있고 나를 위해 일하는 해결책을 찾지 못했다.양식 데이터가 yii2.0 고급 템플릿에 저장되지 않습니까?
My Controller
namespace frontend\controllers;
use Yii;
use frontend\models\Newsletter;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* NewsletterController implements the CRUD actions for Newsletter model.
*/
class NewsletterController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Newsletter models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Newsletter::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Newsletter model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Newsletter model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Newsletter();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
My Model:
<?php
namespace frontend\models;
use Yii;
use yii\web\UploadedFile;
/**
* This is the model class for table "newsletter".
*
* @property integer $id
* @property string $title
* @property integer $bulletin_id
* @property string $image
* @property string $url
* @property string $created
*/
class Newsletter extends \yii\db\ActiveRecord
{
public $image;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'newsletter';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['title', 'bulletin_id', 'image', 'url'], 'required'],
[['bulletin_id'], 'integer'],
[['title', 'url'], 'string', 'max' => 100],
[['image'], 'safe'],
[['image'], 'file', 'extensions'=>'jpg, gif, png'],
[['created'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
//echo "labels";
//exit;
return [
'id' => 'ID',
'title' => 'Title',
'bulletin_id' => 'Bulletin ID',
'image' => 'Image',
'url' => 'Url',
'created' => 'Created',
];
}
}
my _form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
//use kartik-v\yii2-widgets\FileInput;
/* @var $this yii\web\View */
/* @var $model frontend\models\Newsletter */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="newsletter-form">
<?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'bulletin_id')->textInput() ?>
<?= $form->field($model, 'image')->fileInput()?>
<?= $form->field($model, 'url')->textInput(['maxlength' => 100]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
난 당신이 실제로 컨트롤러에서 파일을 받고되지 않습니다