0
votes

I have 2 tables: Order(s) and Product(s). And I have a 3rd one that connects them: OrdersProducts. I would like to save several models at once into OrdersProducts like so:

I would like to select one single Order nr, and from a grid, select several products, adjust amounts and then save all.

My problem is, that I can't make it to show to the user in an appropriate manner if there is a validation error.

The best would be, to show it right there on the form, but I have no clue how to include the 3rd model into the form. I already have the form, and I can save several (or multiple) models at once, but if there is an error, for example: an amount is missing, I don't know how to tell it to the user.

Right now the save is finished, except the ones with errors, but this way the user would have to check all the time if all rows managed to be saved, what would be a nonsense.

The best would be to show the user right there on the form, highlight the problematic row in the grid (or any other way show that there is a problem with this or that particular row or field)

Does it make any sense? If yes, what can be a good (and simple...?) solution for this?

Thanks a lot! BR c

3
How are you showing the inputs? Can you show some of the code? - Mihkel Viilveer
it's a simple dropdown on the top for Orders, and a simple grid widget extended with checkboxes and textfields based on this article: yiiframework.com/wiki/353/working-with-cgridview-in-admin-panel - user2511599
I guess it would be somehow necessary to create as many models for OrdersProducts, as many rows are filtered in the grid. But I don't have yet any clues how to do it, but I keep searching in the meanwhile. - user2511599
IT WORKS! I've managed to figure it out how to make it. It's a little bit tricky and not a very elegant solution, but it basically works as I expect it. Only a few small flaws left to polish. yiiframework.com/forum/index.php/topic/… - user2511599

3 Answers

0
votes

It took reading this a couple times before I realized what you are doing. This is a basic order entry problem. Typically this is solved by creating the order, and then creating the order details one by one. You can show them on the grid as they are created. The thing that confused me was that you seem to have a bunch of empty orders already created, and are trying to do some sort of many to many assignment of products to the orders. That is complicating things far too much.

Better to:

  1. Create a new order. This can be when the user selects the first item to put into a shopping cart, or simply by initiating a create order screen which populates the order header information.
  2. Add an item to the newly created order. Once again this can be that first item added to the shopping cart, or you can just click a button to bring up a create form that simply adds a single item to the order. As each product is added, you can perform your validations for that product.
  3. Repeat step 2 until all products are added to the order.

The create form can be embedded into the page with the grid view and header attributes, and can be posted using an ajax process to allow the grid to be updated for each product added to the order, or it can be a discrete form on a separate page, or it can be a thing where you return to a product page and select the next product like a shopping cart. But in each case you are only adding and validating a single product at a time. That many to many selection thing really only works well for a configuration scenario where you have a small number of values to select from and the only validation necessary is that a selection is required or not.

0
votes

Use a CFormModel to validate the data entered by the user. Add a function to your form model which validates the data and then stores the data in different models. To capture unforeseen errors at the database level you should use transactions.

0
votes

IT WORKS! I've managed to figure it out how to make it. It's a little bit tricky and not a very elegant solution, but it basically works as I expect it. Only a few small flaws left to polish. http://www.yiiframework.com/forum/index.php/topic/44002-multiple-model-save/ If anybody finds any mistakes in the theory, please let me know.