Using BeforeSave

Hello, everybody. i am beginner learning Yii. Please help me for this case.

i have a table (‘Ms_TypeProduct’) and have column :

TypeProduce_ID (Primary Key)

I want to TypeProduct_ID is TP01, TP02, bla bla blaaa (this is automatic when i klik button save)

so i am using beforesave function. but i don’t know how to call it in actioncreate function.

can anyone help me?

this below is my syntax :




use yii\db\Query;

use yii\db\BaseActiveRecord;

public function beforeSave()

    {

        if(parent::beforeSave())

        {

            if($this->isNewRecord)

            {

                $query = new Query;

                $query->select('max(TypeProduct_ID) as TypeProduct_ID')->from('ms_typeproduct')->limit(1)->Scalar();

                $UrutTP= 'TP' + substr('00',intval($query)+1);

            }

            return parent::beforeSave();

        }


public function actionCreate()

    {

        $model = new MsTypeProduct();


        if ($model->load(Yii::$app->request->post())) {

            $model->TypeProduct_ID=$this->beforeSave();

            $model->save();

            return $this->redirect(['view', 'id' => $model->TypeProduct_ID]);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



Thank you if anyone can help me this case. ^^

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.

$insert should be there. But beforeSave is an active Record hook which should not be called Directly.

The Function will run when you call save() in your model.

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.

can help me again?

Try something like this:




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,

            ]);

        }

    }



Thank you for your reply.

Now i am get error when want save.

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);

    }



can help me again? i am using yii 2. thank you

sounds like your Query is expecting an Integer but is getting a CHAR,

is it set as an ‘Integer’ in your db, or as a CHAR?

You should make sure that the query gets the type that it expects or that you make it the right type it requires to make the query work.

(sorry i cannot actually help you with code here)