/* Database Exception – yii\db\Exception
Undefined offset: 1
Failed to prepare SQL: INSERT INTO `article` (`atitle`, `alink`, `atext`, `aimage`) VALUES (:qp0, :qp1, :qp2, :qp3)
↵
Caused by: yii\base\ErrorException
Undefined offset: 1
in C:\wamp\www\yii_yii\advanced\vendor\yiisoft\yii2\db\Command.php at line 282 */
$_GET = [
'r' => 'article/create',
];
$_POST = [
'_csrf-backend' => 'UU9QSjdIRTE3DAApbxA/cjgMJjB.HnxrYQgmFQcZMUk1Fz0BdS42dg==',
'Article' => [
'atitle' => 'tetst',
'alink' => 'tststs',
'atext' => '<p>stststs</p>',
'aimage' => [
'',
],
],
];
$_FILES = [
'Article' => [
'name' => [
'aimage' => [
'15826673_1248634195213700_1569939437665228244_n.jpg',
'15826788_1436518559693668_5787285101862799918_n.jpg',
],
],
'type' => [
'aimage' => [
'image/jpeg',
'image/jpeg',
],
],
'tmp_name' => [
'aimage' => [
'C:\\wamp\\tmp\\phpC5D0.tmp',
'C:\\wamp\\tmp\\phpC5E1.tmp',
],
],
'error' => [
'aimage' => [
0,
0,
],
],
'size' => [
'aimage' => [
41133,
143244,
],
],
],
];
$_COOKIE = [
'advanced-backend' => 'hcfmm57n53gf926s0h3t3t3sh4',
'_csrf-backend' => '1ea9882e6d5d791210cbe0dec331cd5055604c7b0458fad44d0d603122e277c8a:2:{i:0;s:13:"_csrf-backend";i:1;s:32:"fCPcXXzCiCvzIV9Z0Gv_0QtxdXmKBfsG";}',
'_identity-backend' => 'f1c5c5cf00766c4f15699a32b4471626683a88a6c0fbaf695436b9b7e0c33093a:2:{i:0;s:17:"_identity-backend";i:1;s:46:"[3,"ceML8v46RVuqBLcbs61JEyT3zLcdI_ux",2592000]";}',
];
//below is my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use modernkernel\tinymce\TinyMce;
/* @var $this yii\web\View */
/* @var $model backend\models\Article */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="article-form">
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'atitle')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'alink')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'atext')->widget(TinyMce::className(),[
'options' => ['rows' => 6],
'clientOptions' => [
'height'=>320,
'menubar'=> true,
'maxlength' => true,
'plugins' => [
"advlist autolink lists link charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste image"
],
'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | removeformat code"
]
]); ?>
<?= $form->field($model, 'aimage[]')->fileInput(['multiple' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
//this is my controller
<?php
namespace backend\controllers;
use Yii;
use backend\models\Article;
use backend\ArticleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* ArticleController implements the CRUD actions for Article model.
*/
class ArticleController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Article models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ArticleSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Article model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Article model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Article();
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$aid = $model->aid;
//echo $mid;
$model->aimage = UploadedFile::getInstances($model,'aimage');
foreach ($model->aimage as $imager) {
$image1 = new Article();
$aid1 = $image1->aid;
$imgName='article_' . $aid1 . '.' . $imager->getExtension();
if(!$image1->save()){
// handle the errors of model.
var_dump($image1->getErrors());
}
if ($image1->save()) {
$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
}
else{}
}
//echo $imgName='article_' . $mid . '.' . $imager->getExtension(); exit;
// $imgName='article_' . $aid . '.' . $imager->getExtension();
//$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
//$model->aimage=$imgName;
//$model->save();
return $this->redirect(['view', 'id' => $model->aid]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Article model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$aid = $model->aid;
//echo $mid;
$imager = UploadedFile::getInstance($model,'aimage');
//echo $imgName='article_' . $mid . '.' . $imager->getExtension(); exit;
$imgName='article_' . $aid . '.' . $imager->getExtension();
$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
$model->aimage=$imgName;
$model->save();
return $this->redirect(['view', 'id' => $model->aid]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Article model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Article model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Article the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Article::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
//this my model
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "article".
*
* @property integer $aid
* @property string $atitle
* @property string $alink
* @property string $atext
* @property string $aimage
*/
class Article extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'article';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['atitle', 'alink', 'atext', 'aimage'], 'required'],
[['atitle'], 'string', 'max' => 50],
[['alink', 'atext', 'aimage'], 'string', 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'aid' => 'Aid',
'atitle' => 'Atitle',
'alink' => 'Alink',
'atext' => 'Atext',
'aimage' => 'Aimage',
[
'atribute'=>'aimage',
'value'=>Yii::getAlias('@articlesImgPath')
]
];
}
}
/ * view * /
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\models\Article */
$this->title = $model->aid;
$this->params['breadcrumbs'][] = ['label' => 'Articles', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="article-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->aid], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->aid], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'aid',
'atitle',
'alink',
'atext',
'aimage', [
'attribute' =>'aimage',
'value'=>Yii::getAlias('@articlesImgUrl').'/'.$model->aimage,
'format'=>['image',['width'=>'100', 'height'=>'100']]
]
],
]) ?>
</div>
/ * controller * /
<?php
namespace backend\controllers;
use Yii;
use backend\models\Article;
use backend\ArticleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* ArticleController implements the CRUD actions for Article model.
*/
class ArticleController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Article models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ArticleSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Article model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Article model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Article();
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$aid = $model->aid;
//echo $mid;
$model->aimage = UploadedFile::getInstances($model,'aimage');
foreach ($model->aimage as $imager) {
$image1 = new Article();
$aid1 = $image1->aid;
$imgName='article_' . $aid1 . '.' . $imager->getExtension();
if(!$image1->save()){
// handle the errors of model.
var_dump($image1->getErrors());
}
if ($image1->save()) {
$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
}
else{}
}
//echo $imgName='article_' . $mid . '.' . $imager->getExtension(); exit;
// $imgName='article_' . $aid . '.' . $imager->getExtension();
//$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
//$model->aimage=$imgName;
//$model->save();
return $this->redirect(['view', 'id' => $model->aid]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Article model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$aid = $model->aid;
//echo $mid;
$imager = UploadedFile::getInstance($model,'aimage');
//echo $imgName='article_' . $mid . '.' . $imager->getExtension(); exit;
$imgName='article_' . $aid . '.' . $imager->getExtension();
$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
$model->aimage=$imgName;
$model->save();
return $this->redirect(['view', 'id' => $model->aid]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Article model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Article model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Article the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Article::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
/ * model * /
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "article".
*
* @property integer $aid
* @property string $atitle
* @property string $alink
* @property string $atext
* @property string $aimage
*/
class Article extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'article';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['atitle', 'alink', 'atext', 'aimage'], 'required'],
[['atitle'], 'string', 'max' => 50],
[['alink', 'atext', 'aimage'], 'string', 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'aid' => 'Aid',
'atitle' => 'Atitle',
'alink' => 'Alink',
'atext' => 'Atext',
'aimage' => 'Aimage',
[
'atribute'=>'aimage',
'value'=>Yii::getAlias('@articlesImgPath')
]
];
}
}
/ *我发布了我的视图,模型和控制器我不知道为什么在存储多个图像时我得到这个未定义的偏移误差。它可以正常上传单张图像* /