我是yii的新手,致力于创建简报应用程序。为此,我使用的是yii2.0高级模板。我已经通过gii CRUD生成了控制器模型和表单。 我有一个带有图像上传的创建表单(_form.php),当我提交此表单时,它将转到我的控制器中的actionCreate()并且它没有进入if($ model-> load(Yii :: $ app-&gt) ; request-> post())&& $ model-> save()){in actionCreate()。我必须将表单数据保存到数据库中。我有很多谷歌,并没有找到任何适合我的解决方案。
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>
请让我知道我哪里出错了
答案 0 :(得分:1)
您实际上并未在控制器中获取该文件。您需要获取该文件,然后对其执行操作。
按照此处的说明操作:https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md