Привет! Есть такая проблема, читал на форумах как положить 2 модели в одну ActiveForm, но так и не смог понять как это сделать.
Код:
Модель
<?php namespace app\models; use Yii; use yii\gii\CodeFile; class BookingHall extends \yii\db\ActiveRecord { public $date_range; /** * @inheritdoc */ public static function tableName() { return 'booking_hall'; } /** * @inheritdoc */ public function rules() { return [ [['user_id', 'admin_id', 'hall_id', 'seatings_id', 'languege_id', 'transport', 'registration', 'baij', 'translators', 'translators_from_id', 'translators_to_id', 'audio', 'photo', 'people', 'dateStart', 'dateEnd', 'duration', 'breakfast_count', 'lunch_count', 'dinner_count', 'env_1', 'env_2', 'env_3', 'env_4', 'env_5', 'env_6', 'env_7', 'env_8', 'env_9', 'env_10', 'env_11', 'created_at', 'updated_at'], 'integer'], [['user_id', 'people', 'dateStart', 'dateEnd', 'time', 'duration', 'name', 'date_range',], 'required'], [['time'], 'safe'], [['additional', 'status'], 'string'], [['price'], 'number'], [['name'], 'string', 'max' => 255], ['languege_id', 'default', 'value' => 0], ['user_id', 'default', 'value' => 0], ['admin_id', 'default', 'value' => 0], ['translators_from_id', 'default', 'value' => 0], ['translators_to_id', 'default', 'value' => 0], ['breakfast_count', 'default', 'value' => 0], ['lunch_count', 'default', 'value' => 0], ['dinner_count', 'default', 'value' => 0], [['translators_from_id', 'translators_to_id'], 'required', 'when' => function ($model) { return $model->translators == 1; }, 'whenClient' => "function (attribute, value) { return $('#translators').is(':checked'); }", ], ]; } }
Вторая модель
<?php
namespace backend\models;
use \yii\db\ActiveRecord;
use Yii;
/**
* This is the model class for table "booking_hall_env".
*
* @property integer $id
* @property string $booking_hall_id
* @property string $environment_id
*/
class BookingHallEnv extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'booking_hall_env';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['booking_hall_id', 'environment_id'], 'required'],
[['booking_hall_id', 'environment_id'], 'string', 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'booking_hall_id' => 'Booking Hall ID',
'environment_id' => 'Environment ID',
];
}
}
Контроллер :
public function actionCreate()
{
$date = Yii::$app->request->getQueryParam('date', date('Y-m-d'));
$hall_id = Yii::$app->request->getQueryParam('hall_id', 0);
$model = new BookingHall();
if ($model->load(Yii::$app->request->post())) {
preg_match_all('/(\d{4}-\d{2}-\d{2}) - (\d{4}-\d{2}-\d{2})/', $model->date_range, $maches);
if (!empty($maches[1][0]) && !empty($maches[2][0])) {
$model->setAttributes([
'dateStart' => strtotime($maches[1][0]),
'dateEnd' => strtotime($maches[2][0]),
]);
}
$model->setAttributes([
'admin_id' => \Yii::$app->user->id,
'created_at' => time(),
'updated_at' => time(),
]);
$model->price = $model->getPrice();
if ($model->save()) {
return $this->redirect(['index']);
}
}
if (empty($model->date_range)) {
$model->date_range = $date . ' - ' . $date;
}
if ($hall_id) {
$model->hall_id = $hall_id;
}
return $this->render('create', [
'model' => $model,
]);
}
Вьюха там обычная, не могу понять как можно использовать 2 модели в одной ActiveForm