Вот моя модель
Код: Выделить всё
<?php
namespace frontend\models;
use Yii;
use yii\web\UploadedFile;
/**
* This is the model class for table "messages".
*
* @property int $id
* @property string $from_id
* @property string $to_id
* @property string $zakaz_id
* @property string $type
* @property string $status
* @property string $message_text
*/
class Messages extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'messages';
}
public $downloads;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['message_text'], 'required'],
[['from_id', 'to_id', 'zakaz_id', 'type', 'status'], 'integer', 'max' => 45],
[['message_text'], 'string', 'max' => 255],
[['downloads'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, txt', 'maxFiles' => 10],
// ['downloads', 'required', 'message' => 'Please choose a username.'],
// [['downloads'], 'file', 'extensions' => ['jpg','png'],'checkExtensionByMimeType'=>false],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'from_id' => Yii::t('app', 'From ID'),
'to_id' => Yii::t('app', 'To ID'),
'zakaz_id' => Yii::t('app', 'Zakaz ID'),
'type' => Yii::t('app', 'Type'),
'status' => Yii::t('app', 'Status'),
'message_text' => Yii::t('app', ''),
'downloads' => Yii::t('app', ''),
];
}
public function upload()
{
if ($this->validate()) {
foreach ($this->downloads as $file) {
$file->saveAs('D:/Develop/eshop/frontend/uploads/' . $file->baseName . '.' . $file->extension);
}
return true;
} else {
return false;
}
}
public function getUser()
{
return $this->hasOne(Users::className(), ['id' => 'user_id']);
}
public function getOrd()
{
return $this->hasOne(OrderGroup::className(), ['id' => 'zakaz_id']);
}
public function getOrgTo()
{
return $this->hasOne(Organization::className(), ['id' => 'to_id']);
}
public function getOrgFrom()
{
return $this->hasOne(Organization::className(), ['id' => 'from_id']);
}
public function getOrder()
{
return $this->hasOne(Order::className(), ['order_group_id' => 'zakaz_id']);
}
}
Код: Выделить всё
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<div class="inner-message bgcolor clearfix">
<?= $form->field($model,'message_text')->textarea(['placeholder'=>'Введите текст нового сообщения','rows' => '6']); ?>
<?= $form->field($model, 'to_id')->hiddenInput()->label(false)->hint(false); ?>
<?= $form->field($model, 'from_id')->hiddenInput()->label(false)->hint(false); ?>
<?= $form->field($model, 'zakaz_id')->hiddenInput()->label(false)->hint(false);?>
<?= $form->field($model, 'type')->hiddenInput()->label(false)->hint(false);?>
<?= $form->field($model, 'status')->hiddenInput()->label(false)->hint(false); ?>
<?= $form->field($model, 'downloads[]')->fileInput(['multiple' => true, 'accept' => "application/pdf/docx"]) ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Отправить'), ['class' => 'btn btn-info']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
Код: Выделить всё
public function actionIndex($id = 0, $orderId = 0)
{
$shops = ArrayHelper::index (Order::find()
->select([ Order::tableName() . '.org_id', Order::tableName() . '.order_group_id'])
->joinWith('position')
->where([Position::tableName() . '.org_id' => OrganizationHelper::getCurrentOrg()->id])
->distinct()
->all(), 'org_id');
$supplier = Organization::findOne(OrganizationHelper::getCurrentOrg()->id);
if ($id == 0)
{
$id = key($shops);
}
$orders = ArrayHelper::index (OrderGroup::find()
->joinWith('orders')
->where(['.org_id' => $shops[$id]->org_id])
->all(), 'id');
if ($orderId == 0)
{
$orderId = key($orders);
}
$messages = Messages::find()
->where(['zakaz_id' => $orders[$orderId]->id])
->all();
$model = new Messages();
if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isPost) {
$model->downloads = UploadedFile::getInstances($model, 'downloads');
if ($model->upload()) {
// file is uploaded successfully
// return;
}
}
if ($model->save()) {
return $this->redirect(['index', 'id' => $id, 'orderId' =>$orderId]);
}
$model->zakaz_id = $orderId;
$model->from_id = OrganizationHelper::getCurrentOrg()->id;
$model->to_id = $id;
return $this->render('index', [
'model' => $model,
'orders' => $orders,
'messages' => $messages,
'supplier' => $supplier,
'shops' => $shops,
'orderId' => $orderId,
'id' => $id,
]);
}