-2

I need to add code to the database like this CN492B9ХХХХХХ where XXXXXX number that begins with 000000 and incremented by 1 for each data. This is my controller

public function actionCreate()
    {
        $model=new DeliveyInfo;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['DeliveyInfo']))
        {
            $model->attributes=$_POST['DeliveyInfo'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

I ad to the model

public function beforeSave() {
    if ($this->isNewRecord)
        $this->code = sprintf('CN492B9%06d', $this->id);
    return parent::beforeSave();
}

But there is no id number. Where can i get it for the new record?

  • I probably would not put it quite right. I need to create a field in a table that will be written in a unique code. – Olga Bielova Dec 28 '13 at 12:47

1 Answers1

2

Don't do that. Have a simple AUTO_INCREMENT column on your database table that provides the incrementing number, and then format it in your front end code so it looks how you need.

$formatted = sprintf('CN492B9%06d', $integer_from_database);
Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128