How to get last insert id in Yii create page - php

I have a model called Invoice.I have made necessary CRUD for Invoice model.The attributes for the Invoice model are as like Invoice Title, Invoice No, Invoice Issue Date, Description.Now I want that when a user will redirect to invoice create page,the input field for Invoice No should be automatically filled in the create page with last inserted id +1 and it should have prefix like INV:.So the input field for Invoice No should be like this INV:12.By this a user don't need to fill the invoice no manually in increasing order.Here is the image for the create page.

First of all==>This is not a good idea to store Invoice number dependent on table's unique id
So you should insert last invoice number's number part+1 with text..
Now first get last invoice model then find its invoice number and populate the field..
In controller..
$model = new Invoice;
$last_invoice = Invoice::model()->find(array('order'=>'id DESC'));
$last_invoice_number = str_replace("INV:", "", $last_invoice->invoice_mumber);
$new_invoice_number = $last_invoice_number+1;
$model->invoice_number = "INV:".$new_invoice_number;
$this->render('create',array('model'=>$model));
EDIT:
Reason why Yii::app()->db->lastInsertId) cannot be used..
First as I said in first place its not good to dependent invoice id from unique id of table..second what if somebody inserted sonething in someother table in between..third what if the db connection is closed and started in between..

Just a Suggestion:
If user does not need to fill the invoice number then why don't you just skip it from Display. I mean let not show it while creating. Once the form is submitted, you can show all the submitted value along the invoice number.
Once the form is submitted, you can display the invoice number simply by $model -> id; where id is the invoice number in the database.
You could use invoice number as the primary auto increment bigint value in the database. Then use INV: prefix while display.
Hope it helps!

This is the simple way to get last insert id in Yii.
Yii::app()->db->getLastInsertId();

Related

Is creating a new table each time a user logs in a bad practice?

I am developing an online inventory management system. I have one table for the main inventory which consists of the product names, quantity and barcodes etc. I want users to log into the system each morning and log the quantity of each item they take. I then want to store this information for the admin to view. I have developed the system so that a new table gets created each morning based on the users name and date. This is allows the user to input the quantity for each product, i then subtract that column from the main inventory table when the user clicks submit. I want to know if this is a bad practice, is it necessary to create a new table each morning? If not what method should i use?
I think you should maintain a single table for inventory, and then after a create a temporary table to store the list of items the user takes which will contain the user id, inventory id, qty etc, and then update the quantity of inventory in inventory table, whenever user picks the inventory.
In short you should create a single table to store the information of inventory which user takes instead of creating a new table each day.
Insert and fetch data according to date + user.
I wonder if you are creating new table everyday, you should not create new table in any case. In the rarest scenario even if your columns are dynamic then create a table with rows representing as column and use pivot to fetch the record. For your use case you just need to have a table which stores a new record and subtract the count from main table.

Submitting the same value many times when the value needs to be unique

I have a form where I have 11 elements, for simplicity purposes I am going to show only three on here.
1 Member Name: => Text field
2 Membership Type: => Select options field
3 Invoice ID: => Text field.
The requirement condition for this form is that the Invoice Id needs to be unique and has a post processor that checks if the Invoice Id already exists in the database. If the Invoice Id is in the database then the form submit returns an error or else it submits. Now the problem I am having with this is that sometimes the User needs to update or change the Membership type without touching the Invoice ID, in these circumstances the form needs to be submitted but the processor blocks it since the Invoice Id will already be in the database.
Note: this form submits to the members table so there is auto increment id number for each record.
So I solved this issue with this way. Since this form relates to a member table, Each Record will have a unique ID. I modified the form post processer to check for every invoice id in the table but the current ID. This way I can make sure the form will be saved every time while having a unique invoice id for each record.

Auto Invoice Number Generation in Laravel 5.2

I am building an invoice application using laravel 5.2
I want to create automatic invoice numbers incrementing. Meaning,
If i open create invoice page, i should automatically get #0001 so when i save the invoice, under my invoice_number field it will be 0001 then when i am creating another invoice, the controller will check for the last invoice_number in the database and automatically give me #0002.
Any idea on how to do that or something similar??
Thanks
Get the last record from the db in descending order.
$invoice = new Invoice();
$lastInvoiceID = $invoice->orderBy('id', DESC)->pluck('id')->first();
$newInvoiceID = $lastInvoiceID + 1;
This will return the last ID. Then add 1 to it, and display it on the front end {{ $newInvoiceID }}.
Possible issue: What if two users are creating invoices simultaneously? Once they save, the numbers will be different if invoice number is a unique identifier.
One option is to create invoices per user.
$invoice = new Invoice();
$lastInvoiceID = $invoice->where('user_id', '=', Auth::user()->id)->orderBy('invoice_id', DESC)->pluck('invoice_id')->first();
$newInvoiceID = $lastInvoiceID + 1;
Auto-increment the invoice_number field in your MySQL table (or just use the 'ID' as your invoice_number:
CREATE TABLE invoices (
ID int NOT NULL AUTO_INCREMENT,
invoice_number NOT NULL AUTO_INCREMENT
)
Every time you add an invoice to the table, MySQL automatically assigns it the next number. So if the last invoice in the table is 3, the next one you insert will be assigned 4.
If you want to display the numbers like #0003 and #0004, just query the database for the invoice_number and format the number in PHP.

How can I use an auto-incremented MySQL key in a new html form?

I have two html forms to register products in an RMA. The first form is really basic and I can easily store the data to the db. The problem comes when I use $newrmaid = $conn->insert_id;(where $conn is a mysqli-object) to get the auto-incremented ID from the db when i insert the data from the first form.
In the second form I need information from the db related to the key in $newrmaid.
I got it to work with a set number of products (combine the two forms), but the RMA can have a dynamic number of products attatched to it.
The idea is that when you register a new RMA you get presented a new page with all products that are attatched to this RMA plus a form for attatching new products. When you insert a new product the page refreshes and you can choose to insert more products.
Any good advice?
I'm not the fan of sending back the inserted_id to the next page. Because it could be different when more than one user are inserting data at the same time.
Retrieve it before loading the form
SELECT id FROM table ORDER BY id DESC LIMIT 0,1

insert a row with one column value based on last record

I need to insert a newly added product in table. But to do so I need to check SKU of last inserted product and increase it by 1 and insert it with new product details. SKU is something like SKUNXXXXX - X is a digit. Changing table structure is not possible.
Possible solutions that I can think of is
Get last row using order by and limit 1.
replace "SKUN" with empty string and increase the number by 1
Insert record with product details and incremented SKU value
But this situation may create a problem(though I am not sure about it). Problem is - what if just after fetching last record and before inserting the new product details, another request comes in and gets the same last record? In this case both of the products have same SKU.
Please let me know how to solve this situation.
One option would be to create an auto increment id column and then use this to create the SKU for each product as it gets inserted. A trigger, which would fire after an INSERT, could then be used to assign the SKU for the newly inserted product. But alas, MySQL does not allow a trigger to modify the table which fired it.
However, there is a potentially easy workaround here. If your SKU values will really always have the format SKUNXXXXX, where XXXXX is some number, then you can simply add an auto increment column, and create the SKU value on the fly when you query for it. So you might create a table called products with the following columns:
CREATE TABLE products
(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(55),
...
);
Everytime you do an INSERT on products, the id column will automatically be incremented by MySQL. When you want to get the SKU out, you can simply do a query like this:
SELECT id, name, CONCAT('SKUN', id) AS `SKU`
FROM products
WHERE name = 'someproduct'
If you want the SKU number to not be forever (i.e. once assigned to a given product, it can be reassigned to another new one), then you might consider resetting the auto increment id column, q.v. this SO post for more information.

Categories