i did CRUD in backend on user table in class extend ActiveRecord. But in my table i have a lot of data which i dont want to give the user to fill for example auth_assignment, createdAt. So i dont know now how i can use function which fill it for user. I know that in SignupForm for example i can use method save() and there i can use function generate auth_key for example. In model i often use this function:
public function saveProfile() {
$model = $this->_user;
$model->Name=$this->Name;
$model->Surname=$this->Surname;
$model->Login=$this->Login;
$model->Rel_Sex= $this->Sex;
$model->setAttributes($this->getAttributes());
$model->BirthDate = $this->getDbFormatedDate();
$model->Rel_Country = $this->Country;
$model->Rel_Language = $this->Language;
$model->Email = $this->Email;
$model->Rel_UserCategory= $this->Category;
$model->setPassword($this->Password);
if ($this->validate() && $model->validate()) {
$model->save();
return $model;
}
return false;
}
But i dont know how to overwirte method save() in ActiveRecord. So i have this class extend ActiveRecord:
<?php
namespace backend\modules\users\models;
use Yii;
use yii\behaviors\TimestampBehavior;
class Uruser extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'uruser';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['Name', 'Surname', 'BirthDate', 'Login', 'Email', 'PasswordHash', 'created_at', 'Rel_Sex', 'auth_key', 'status', 'auth_assignment'], 'required'],
[['BirthDate'], 'safe'],
[['RulesAccept', 'created_at', 'updated_at', 'Rel_Sex', 'Rel_Country', 'Rel_Language', 'Rel_UserCategory', 'Rel_AccountType', 'Rel_RoyalUserData', 'Rel_EmailPermission', 'Rel_CommercialPermission', 'IsDeleted', 'status'], 'integer'],
[['Name', 'Surname', 'Login', 'Email', 'PasswordResetToken', 'auth_key', 'auth_assignment'], 'string', 'max' => 45],
[['PasswordHash'], 'string', 'max' => 90]
];
}
And i fill only email, birthdate, login, name. But i want to use some function to fill my attributes on save. For example generate AuthKey.
public function setPassword($password)
{
return $this->PasswordHash = Yii::$app->security->generatePasswordHash($password);
}
public function generateAuthKey()
{
return $this->auth_key = Yii::$app->security->generateRandomString();
}
How i can do this on save?
When i used beforeSave it not work:
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if($this->isNewRecord) {
$this->generateAuthKey();
$this->setPassword($this->PasswordHash);
$this->status=1;
$this->created_at=date('Y-m-d');
}
return true;
} else {
return false;
}
}
I var_dum in controller:
public function actionCreate()
{
$model = new Uruser();
var_dump($model->load(Yii::$app->request->post()), $model->save(),$model->getErrors());
exit();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->Id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
An i have this:
bool(true) bool(false) array(3) { ["created_at"]=> array(1) { [0]=> string(27) "Created At cannot be blank." } ["auth_key"]=> array(1) { [0]=> string(25) "Auth Key cannot be blank." } ["status"]=> array(1) { [0]=> string(23) "Status cannot be blank." } }
When i used beforeSave now when i update password then it not hashed my password and sawe this waht i wrote. I try this in my Model but it isnt work:
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if($this->isNewRecord) {
$this->created_at = date('Y-m-d H:i:s');
$this->status = self::DEFAULT_STATUS;
$this->generateAuthKey();
$this->setPassword($this->PasswordHash);
$this->RulesAccept=1;
}
return true;
} else {
$this->setPassword($this->PasswordHash);
$this->updated_at = date('Y-m-d H:i:s');
return true;
}
}
AFTER EDIT
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if($this->isNewRecord) {
$this->created_at = date('Y-m-d H:i:s');
$this->status = self::DEFAULT_STATUS;
$this->generateAuthKey();
$this->setPassword($this->PasswordHash);
$this->RulesAccept=1;
return true;
}
else {
$this->setPassword($this->PasswordHash);
$this->updated_at = date('Y-m-d H:i:s');
return true;
}
}
}
this work for me