【发布时间】:2018-03-29 09:31:14
【问题描述】:
我在创建帖子时尝试上传多张图片,但只有一张图片存储在数据库中,所有图片都保存在服务器中,但在我的表中只保存第一张图片但不是全部,所以我创建了上传功能
// Function to upload images
public function CreateGallery($model_Post, $model_Gallery)
{
$model_Post->Post_id = md5(microtime());
$model_Post->User_id = Yii::$app->user->id;
$GLRID = md5(uniqid());
// file
$GFolder = 'upload/images/'. $model_Post->Post_id .'/' ;
mkdir ($GFolder, 0777, true);
if ( $model_Post->save() ){
$model_Gallery->GalleryFile = UploadedFile::getInstances($model_Gallery, 'GalleryFile');
$ly_GalName = uniqid();
foreach ($model_Gallery->GalleryFile as $GalleryImage) {
++$ly_GalName;
$model_Gallery->Gallery_id = ++$GLRID;
$model_Gallery->User_id = $model_Post->User_id;
$model_Gallery->Post_id = $model_Post->Post_id;
$GalleryImage->saveAs($GFolder . $GalName . '.' . $GalleryImage->extension);
$model_Gallery->Gallery_image = $GFolder . $GalName . '.' . $GalleryImage->extension;
}
$model_Gallery->save(false);
}
}
在我的控制器中,我添加了我创建的函数
public function actionCreate()
{
$model_Post = new Post();
$model_Gallery = new Gallery;
$actions_UploadImages = new ActionsUploadImages();
if ($model->load(Yii::$app->request->post()) ) {
$actions_UploadImages->CreateGallery($model_Post, $model_Gallery)
return $this->redirect(['view', 'id' => $model_Post->Post_id]);
} else {
return $this->render('create', [
'model_Post' => $model_Post,
'model_Gallery' => $model_Gallery,
]);
}
}
我的观点/创建我的代码是
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model_Post, 'Post_title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model_Gallery, 'GalleryFile[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
<?= $form->field($model_Post, 'Post_text')->textarea(['rows' => 2]) ?>
<?= $form->field($model_Post, 'Permission_id')->dropdownList($model_Permission->PermissionList()) ?>
<div class="form-group">
<?= Html::submitButton($model_Post->isNewRecord ? 'Create' : 'Update', ['class' => $model_Post->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
我尝试将 gallery_id 更改为 AUTO_INCREMENT 但仍然是同样的问题,我尝试删除 if ( $model_Post->save() ),但我从数据库中收到错误消息。就像现在一样,它将所有图像保存在服务器上,但在表格中只保存一张图像。
//// 画廊模型:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "gallery".
*
* @property string $Gallery_id
* @property string $Gallery_image
* @property string $Post_id
* @property string $User_id
*
* @property Post $post
* @property Userlogin $user
*/
class Gallery extends \yii\db\ActiveRecord
{
public $GalleryFile;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'gallery';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['Gallery_id', 'Post_id', 'User_id'], 'required'],
[['Gallery_id'], 'string', 'max' => 200],
[['Gallery_image'], 'string', 'max' => 350],
[['Post_id', 'User_id'], 'string', 'max' => 300],
[['GalleryFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 5],
[['Post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['Post_id' => 'Post_id']],
[['User_id'], 'exist', 'skipOnError' => true, 'targetClass' => Userlogin::className(), 'targetAttribute' => ['User_id' => 'User_id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'Gallery_id' => 'Gallery ID',
'Gallery_image' => 'Gallery Image',
'Post_id' => 'Post ID',
'User_id' => 'User ID',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPost()
{
return $this->hasOne(Post::className(), ['Post_id' => 'Post_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(Userlogin::className(), ['User_id' => 'User_id']);
}
/**
* @inheritdoc
* @return \app\queries\GalleryQuery the active query used by this AR class.
*/
public static function find()
{
return new \app\queries\GalleryQuery(get_called_class());
}
}
【问题讨论】:
-
它打印什么错误请添加它显示的eaxct异常或模型错误
-
它说的错误:完整性约束违规:1452 无法添加或更新子行:外键约束失败
-
添加您的画廊模型
-
我确实添加了我的模型
标签: yii2 yii2-basic-app