0
我是yii的新人,致力於創建新聞通訊應用程序。爲此,我使用yii2.0高級模板。我已經通過gii CRUD生成了控制器模型和表單。 我有一個創建窗體(_form.php)與圖像上傳時,我提交這種形式它將要actionCreate()在我的控制器,它不進入if($模型 - >加載(Yii :: $ app-> request-> post())& & $ model-> save()){in 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>
請讓我知道我錯在哪裏