A
A
AtomU2352018-06-15 05:45:19
Yii
AtomU235, 2018-06-15 05:45:19

Yii 2 - How to organize the interaction of a model and a subordinate model on one form?

Greetings.
Task. There is a model-1 (owner) and an associated model-2 (slave). One instance of the model is displayed on the form as model-1, for example, the date field and model-2 as a set of model instances in the gridView. If anyone is familiar with 1C, it is very similar to the document and the tabular part of the document.
Question: 1. How can I add/delete/edit rows in a subordinate table without having a saved record in the owner table yet. That is, the id of Model-1 is not yet known.
2. Fix in the database all manipulations with model-2 (adding/deleting/editing rows) only after saving model-1.
UPD: I'm doing this now. the first step I save the record from model-1 and only then I give the opportunity to work with model-2. Each change in model-2 is immediately saved in the database.
UPD 2: 5b239d191bb67531078097.jpeg
For clarity. Yellow - Model-1, green - model-2
In other words, I would like to make model-2 entries and that they would be saved only when I click "Save" In general, the most common behavior for desktop applications. due to inexperience or inertia in web development, this baffles me. Seems like a simple thing :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2018-06-15
@masterfreelance

1. Nothing. And in the vast majority of cases, it is unnecessary to do this, it violates the integrity of the database. In 1C, when saving, first a record is created for the document, and then a tabular part for this document. The fact that you see the fields of the new document and the fields of the tabular section on the client does not mean that all this is already in the database. And of course all this is wrapped in a transaction.
2.Again transactions. Failed to commit changes to model1 - rollback to initial state.

M
Maxim Timofeev, 2018-06-15
@webinar

A lot of text, but in fact I understand that the problem is only with the lack of an id for the new model. It is solved either:
- immediately save empty or demo data, then render the form
- send everything at the same time, while saving the associated data occurs in the afterSave of the parent, that is, the names of the fields of the daughters of the type ParentModelName[someChildModelName][AttrinuteName], respectively, the data through the load of the parent fall into the public attribute of the parent and is available d afterSave via $this->someChildModelName.
There is a link method, here: https://www.yiiframework.com/doc/guide/2.0/en/db-a...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question