I am new to this as well, so probably can’t be of too much help. But I did notice that you are missing the $insert argument in your beforeSave declaration.
public function beforeSave($insert){
//
}
I can’t tell from the API whether $insert is required. I suppose if you leave it out, it would be like passing a false $insert, so the event would only trigger on update.
Thank you for your respon all. but how to get value typeproduct_id. because i get error "typeproduct_id can not blank/null". it is see like i dont get value in typeproduct_id.
public function beforeSave($insert) {
if ($insert) {
$query = new Query;
$query->select('max(TypeProduct_ID) as TypeProduct_ID')->from('ms_typeproduct')->limit(1)->Scalar();
$UrutTP= 'TP' + substr('00',intval($query)+1);
$this->TypeProduct_ID = $UrutTP;
}
return parent::beforeSave($insert);
}
public function actionCreate()
{
$model = new MsTypeProduct();
if ($model->load(Yii::$app->request->post())) {
$model->save();
return $this->redirect(['view', 'id' => $model->TypeProduct_ID]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
i get this error "Object of class yii\db\Query could not be converted to int"
Typeprduct_ID is CHAR(4) type data.
public function beforeSave($insert) {
if ($insert) {
$query = new Query;
$query->select('max(TypeProduct_ID) as TypeProduct_ID')->from('ms_typeproduct')->limit(1)->Scalar();
$UrutTP= 'TP' + substr('00',intval($query)+1);
$this->TypeProduct_ID = $UrutTP;
}
return parent::beforeSave($insert);
}