Hi guys, I used CRUD and I created two models. I tried to create two forms in one view. So in my controller I created new models and wrote a check if a form is filled to record it and if two forms to save two. But It not work for me. It is my controller actionCreate:
public function actionCreate()
{
$model = new UrUserForm();
$userDate= new UserDataForm();
$model->scenario = 'create';
if ($model->load(Yii::$app->request->post())) {
try {
$model->saveUser();
} catch (Exception $ex) {
Yii::$app->session->setFlash('error', Yii::t('app', 'Dane nie zostały zapisane.'));
}
return $this->redirect(['index']);
} elseif(($userDate->load(Yii::$app->request->post()) && $model->load(Yii::$app->request->post()))){
try {
var_dump( $model->saveUser(), $userDate->saveOptionalData());
exit();
$model->saveUser();
$userDate->saveOptionalData();
} catch (Exception $ex) {
Yii::$app->session->setFlash('error', Yii::t('app', 'Dane nie zostały zapisane.'));
}
} else {
return $this->render('create', [
'model' => $model,
'userDate'=> $userDate
]);
}}
This var_dump is not displayed when i fill both forms. So i think i do some wrong in this controller. Maybe i show You My ModelsForm for sure:
<?php
namespace backend\modules\users\models;
use common\models\User;
use backend\modules\users\models\UrUser;
use yii\base\Model;
use Yii;
use yii\helpers\Url;
/**
* Signup form
*/
class UrUserForm extends Model {
public $Login;
public $Email;
public $Password;
public $Sex;
public $Country;
public $Language;
public $Category;
public $AccoutType;
public $Name;
public $Surname;
public $BirthDate;
public $RulesAccept;
public $user;
/**
* @inheritdoc
*/
public function rules() {
return [
[['Country', 'Language', 'Category'], 'safe'],
['Login', 'filter', 'filter' => 'trim'],
[['Login', 'Sex', 'Name', 'Surname', 'BirthDate'], 'required'],
['Login', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'Pdany login jest już używany')],
['Login', 'string', 'min' => 2, 'max' => 255],
['Email', 'filter', 'filter' => 'trim'],
['Email', 'required'],
['Email', 'email'],
['BirthDate', 'ageValidator'],
['Email', 'string', 'max' => 255],
['Email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'Podany e-mail jest już używany')],
['Password', 'string', 'min' => 6],
['Password', 'required','on' => 'create'],
];
}
public function saveUser() {
$user = new UrUser();
$user->Login = $this->Login;
$user->Email = $this->Email;
$user->RulesAccept = 1;
$user->Rel_Sex = $this->Sex;
$user->Name = $this->Name;
$user->BirthDate = $this->BirthDate;
$user->Surname = $this->Surname;
$user->setPassword($this->Password);
$user->generateAuthKey();
$user->Rel_Country = $this->Country;
$user->Rel_UserCategory = $this->Category;
$user->Rel_Language = $this->Language;
$user->status = 10;
$this->user = $user;
$user->created_at=time();
if ($this->validate() && $user->validate()) {
$user->save();
return $user;
}
return false;
}
public function updateUser() {
$this->user->load($this->toArray(), '');
$this->user->Rel_Country=$this->Country;
$this->user->Rel_Language=$this->Language;
$this->user->Rel_UserCategory=$this->Category;
$this->user->Rel_Sex=$this->Sex;
$this->user->updated_at=time();
if (!empty($this->Password)) {
$this->user->setPassword($this->Password);
}
return $this->user->save();
}
And second model:
<?php
namespace backend\modules\users\models;
use common\models\UserData;
use frontend\modules\settings\models\Profile;
use yii\base\Model;
use Yii;
/**
* Signup form
*/
class UserDataForm extends Model
{
public $Address;
public $NIP;
public $CompanyName;
public $Website;
public $Phone;
public $IsCompany;
public $IsPhoneConfirmed;
public $CreatedAt;
public $UpdateAt;
public $Rel_State;
public $Rel_Currency;
public $IsDeleted;
public $Id;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['Address', 'Phone', 'Rel_State', 'Rel_Currency'], 'required'],
[['NIP','Id', 'Phone', 'IsCompany', 'IsPhoneConfirmed', 'CreatedAt', 'UpdateAt', 'Rel_State', 'Rel_Currency', 'IsDeleted'], 'integer'],
[['Address', 'CompanyName', 'Website'], 'string', 'max' => 45]
];
}
public function saveOptionalData() {
$model = new UserData();
//$user=
$model->Address=$this->Address;
$model->Phone=$this->Phone;
$model->Rel_State=$this->Rel_State;
$model->Rel_Currency= $this->Rel_Currency;
$model->NIP=$this->NIP;
$model->IsCompany = $this->IsCompany;
$model->IsPhoneConfirmed = $this->IsPhoneConfirmed;
$model->CompanyName = $this->CompanyName;
$model->Website = $this->Website;
if ($this->validate() && $model->validate()) {
//$user->Rel_RoyalUserData=$model->Id;
$model->save();
return $model;
}
return false;
}
And my view Create:
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
<hr>
<?= $this->render('_form2', [
'userDate'=> $userDate
]) ?>
Anyone can help me? I new in Yii but i tried to search for answer in documentation but i failed.