sefti
(Msiswasefti Lpkia)
August 27, 2015, 2:08am
1
Hi im newbie , and a have one question!
How to generate code automatically like
SPPB1508/001
SPPB
it is name of code,
1508
it is year and month,
001
it is a number that will increase when the code contained in the database.
table :
SPPB
id
no_sppb
nama_sppb
no_sppb will be filled by that code.
I hope you will help me.
sorry for my bad english , I translate it in google translate
amir.duran
(Amir Duran)
August 27, 2015, 12:57pm
2
Welcome to the Yii forum.
The first place to start is to learn what is Gii, and how to make models and CRUD operations uisng Gii tool.
Here is a good tutorial: http://code-epicenter.com/what-is-gii-and-how-to-use-it/
Using Gii tool you can automatically make INSERT, UPDATE, DELETE, SEARCH functionalities, which will save you a lot of time.
softark
(Softark)
August 27, 2015, 2:21pm
3
I would do it in CActiveRecord::beforeSave() like the following:
protected function beforeSave()
{
if (parent::beforeSave()) {
if ($this->isNewRecord) {
$name = 'SPPB';
$ym = substr(date('Ym'), 2, 4);
$code = sprintf('%03d', SerialNumber::generateSerialNumber($name, $ym));
$this->no_sppb = $name . $ym . '/' . $code;
}
return true;
} else {
return false;
}
}
SerialNumber model is responsible for generating a serial number …
public static function generateSerialNumber($name, $ym)
{
$model = SerialNumber::model()->find(
'name = :name AND ym = :ym',
array(':name' => $name, ':ym' => $ym));
if (!$model) {
$model = new SerialNumber();
$model->name = $name;
$model->ym = $ym;
$model->number = 0;
}
$number = $model->number + 1;
$model->number = $number;
$model->save();
return $number;
}
Well, please note that the generateSerialNumber() method above is not perfect. It may return a wrong serial number when your app get multiple requests at the same time.
Please google for ‘LAST_INSERT_ID’ for the tips to correct the method, though it is MySQL specific.
mrafa2525
(Ahmad Arley)
September 4, 2015, 9:56am
5
copy paste this code in your model
public function generateCode() {
$_d = date("ym");
$_i = "SPPB.";
$_left = $_d;
$_first = "001";
$_len = strlen($_left);
$no = $_i . $_left .'/'. $_first;
$last_po = $this->find(
array(
"select" => "id",
"condition" => "left(id, " . $_len . ") = :_left",
"params" => array(":_left" => $_left),
"order" => "id DESC"
));
if ($last_po != null) {
$_no = substr($last_po->id, $_len);
$_no++;
$_no = substr("000", strlen($_no)) . $_no;
$no = $_i . $_left .'/'. $_no;
}
return $no;
}
and call this function like :
protected function beforeSave() {
if ($this->isNewRecord) {
$this->id = $this->generateCode();
.... // any code that you want to
} else {
.... // any code that you want to
parent::beforeSave();
}
return true;
}
this code work for me.
sefti
(Msiswasefti Lpkia)
September 7, 2015, 1:54pm
6
copy paste this code in your model
public function generateCode() {
$_d = date("ym");
$_i = "SPPB.";
$_left = $_d;
$_first = "001";
$_len = strlen($_left);
$no = $_i . $_left .'/'. $_first;
$last_po = $this->find(
array(
"select" => "id",
"condition" => "left(id, " . $_len . ") = :_left",
"params" => array(":_left" => $_left),
"order" => "id DESC"
));
if ($last_po != null) {
$_no = substr($last_po->id, $_len);
$_no++;
$_no = substr("000", strlen($_no)) . $_no;
$no = $_i . $_left .'/'. $_no;
}
return $no;
}
and call this function like :
protected function beforeSave() {
if ($this->isNewRecord) {
$this->id = $this->generateCode();
.... // any code that you want to
} else {
.... // any code that you want to
parent::beforeSave();
}
return true;
}
this code work for me.
This code works, but stopped at number 10 and can not increase