I'm using symfony ODM, I have two tables one is for saving bills another is for saving last bill number.
Bills
id|bill_no |created_at
--|-------------------
1 |b2018/01|ISODate("2019-03-18T22:05:17.000+05:30")
--|-------------------
2 |b2018/01|ISODate("2019-03-18T22:05:17.000+05:30")
--|-------------------
3 |b2018/02|ISODate("2019-03-18T23:06:12.000+05:30")
Last_Bill
id|last_bill_no|key |last_updated_at
--|-------------------
1 |2 |BILL |ISODate("2019-03-18T23:06:12.000+05:30")
Based on Last_Bill table i'm saving bill_no to bills table, if i create two bills at the same time then bill number duplication occurs, how i can avoid this duplication it? Please help me. my code is given below
$dm = $this->get('doctrine_mongodb')->getManager();
$lastBill = $dm->getRepository('AppBundle:LastBill')->findOneByKey('BILL');
$billNo = b.''.date('Y').'/'.($lastBill->getLastBillNo()+1);
$bill = new Bills();
$bill->setBillNo($billNo)
$dm->persist($bill);
$lastBill->setLastBillNo($lastBill->getLastBillNo()+1);
$dm->flush();
In Bills Document billNo is defined as,
/**
* @MongoDB\Field(name="bill_no", type="string")
*/
protected $billNo;
You can solve this issue by adding @MongoDB\Index(unique=true) decorator to billNo field.
In Bills Document billNo will be defined as,
/**
* @MongoDB\Field(name="bill_no", type="string")
* @MongoDB\Index(unique=true)
*/
protected $billNo;
In your controller, you should use ensureIndexes() function to ensure index creation and you should wrap your code in try...catch to avoid duplicate index exception in case you save two entities with the same billNo.
Your controller will be similar to this:
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->getSchemaManager()->ensureIndexes();
$lastBill = $dm->getRepository('AppBundle:LastBill')->findOneByKey('BILL');
$billNo = b.''.date('Y').'/'.($lastBill->getLastBillNo()+1);
$bill = new Bills();
$bill->setBillNo($billNo)
$dm->persist($bill);
$lastBill->setLastBillNo($lastBill->getLastBillNo()+1);
try
{
$dm->flush();
} catch (\Exception $e) {
// write some code here based what you want.
}