Universal reusable data forms. Cooking and serving recipes

Data in any application is a key factor in its ease of understanding. Over time, a specific approach to their selection, processing and updating is developed. If, while processing the data, you are still thinking about how to store your data, how to choose what you need from the list, perhaps this approach will simplify your life. The basic requirements for data storage could sound something like this:

The structure is as close to relational as possible. This form of data storage has gained popularity among developers and we will not refuse it.

The data should provide ease of access to the main element by identifier. Relationships with adjacent elements are based on the use of a secondary key as a pointer to the main element. Quick access will be a plus.

Reuse of the data list. Once a data slice has been received, there should be an opportunity, access to any of the elements by its main key or by coincidence with the value of one or more attributes, as well as building lists from the selection.

An additional bonus will be the use of "on the fly" that will allow you to access objects through several levels.

Unambiguous support for the transition through any of the possible connections: one to many, many to one and many to many. The ability to quickly modify the connection logic with further changes to the data structure.

After numerous trial and error, I found a form that suited me a little less than completely. Conditionally, let's call it the “Basic Form”. Visually, the data of the main form can be represented by an array of keys which will be the values ​​of the auto-increment field of the table, and the values ​​are a list of record fields of the selected table. To select a similar structure from the database, we will use the function conditionally called rebuild (rebuild) rb ():

pre (rb ("marks"));

Array(
    [1] => Array([id] => 1, [name] => Вольво)
    [2] => Array([id] => 2, [name] => Мерседес)
    [4] => Array([id] => 4, [name] => Ситроен)
)

Unique auto-increment fields guarantee us conflicts in the names of data records. They will further help in the formation of dynamic filters, so their use in the future is also justified.

A related model table could look like this:

pre (rb (“models”));

Array(
    [1] => Array([id] => 1, [marks_id] => 1, [name] => Volvo S60, [price] => 1644000)
    [2] => Array([id] => 2, [marks_id] => 1, [name] => Volvo V40, [price] => 1309000)
    [3] => Array([id] => 3, [marks_id] => 2, [name] => CLS Shooting Brake, [price] => 3520000)
)

The further idea of ​​using such constructions comes down to a consistent indication of the fields by which we select and the values ​​for the selection. A field is considered a text field, array keys or numerically are considered a value for selection. At the same time, the selection of all the values ​​we need can occur without transferring the code to the controller or it can be done directly in the template.

Display a list of models in which there is at least one brand and a list of these brands.


image

This approach allows you to implement simple linear logic with virtually no dependencies and quickly modifiable. The set of keys in the parameters of the reassembly function is the ease of perception of the functional and its modification.

At the same time, all Volvo models are selected as follows, where the brand identifier is obtained from the Get page request:



Array
(
    [1] => Array([id] => 1, [marks_id] => 1, [name] => Volvo S60, [price] => 1644000)
    [2] => Array([id] => 2, [marks_id] => 1, [name] => Volvo V40, [price] => 1309000)
)

Similarly, you can select under other fields. A selection of a model by its id is as follows:

where $ _GET ['id'] = 2;

Array
(
    [2] => Array([id] => 2, [marks_id] => 1, [name] => Volvo V40, [price] => 1309000)
)

Choose one divided each brand:



Array
(
    [1] => Array([id] => 2, [marks_id] => 1, [name] => Volvo V40, [price] => 1309000)
    [2] => Array([id] => 3, [marks_id] => 2, [name] => CLS Shooting Brake, [price] => 3520000)
)

In a similar way there is an appeal from the model to the make of the machine.


Array(
    [1] => Array([id] => 1, [name] => Вольво)
)

Despite some drawbacks, this approach allows you to close data requests using the standard first form by 90%.
I hope this approach helped someone to make life easier, and the data is clearer. It simplifies life that you always get a form similar to the one you would get without parameters. The logic of the application becomes significantly predictable.

Well, actually the implementation of this function .

Also popular now: