Reputation: 1520
Im using rand() function to generate booking_id
in my app, its generating random string while creating the booking record. But when I update the particular record booking_id
also get updated to some random string. How to stop generating random number while updating the records.
<?= Html::activeHiddenInput($model, 'booking_id', ['value' => rand('100000',10) ]) ?>
Upvotes: 1
Views: 559
Reputation: 1663
use behaviors to set the booking id e.g in this example set the booking_id
with your rand value and this only for the insert event so on update the booking_id
will be untouched.
Add the behaviors code to your model (or extend your existing behavior with it):
namespace app\models\base; // or whatever your namespace is.
use yii\behaviors\AttributeBehavior; // must be added to the use part to include the correct class.
use yii\db\ActiveRecord;
class Yourclass extends ActiveRecord
{
...
public function behaviors()
{
return [
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['booking_id'],
],
'value' => function ($event) {
return rand('100000',10);
},
],
];
}
...
}
And don't use hidden field from your form. The Html Code could be modified so hidden field isn't secure to prevent user modification.
Upvotes: 1