📜 ⬆️ ⬇️

Universal reusable data forms. Cooking and serving recipes

Data in any application is the key to ease of understanding. Over time, a certain approach is developed for their selection, processing and updating. If processing data you still think about the form in which to store your data, how to choose from the list what you need - perhaps this approach will simplify your life. The basic requirements for data storage might sound something like this:

The structure is as close as possible to the relational one. 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 data list. Once a data slice is received, the opportunity should remain, accessing any of the elements by its primary key or coincidence with the value of one or several attributes, as well as the construction of lists from the sample.

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

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

After numerous trials and errors, I found a form that suited me a little less than completely. Conventionally, let's call it the "main 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 ​​- the list of fields of the entry of the selected table. To select a similar structure from the database, we will use the function conditionally called rebuild (reassembly) 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 data record name conflicts. They will further help in the formation of dynamic filters, so their use in the future is also justified.

A related model table would 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) ) 

A further idea of ​​using such constructions comes down to the sequential indication of the fields for which we select and the values ​​to be sampled. The field is a text field, the keys of an array or numeric are considered a value for the sample. In this case, the sampling of all the values ​​we need can occur without carrying the code into the controller and 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.

 <? foreach(rb("marks", "id", "id", rb("models", "marks_id")) as $marks): ?> <h1><?=$marks['name']?></h1> <? foreach(rb("models", "marks_id", "id", $marks['id']) as $models): ?> <div><?=$models['name']?></div> <? endforeach; ?> <? endforeach; ?> 

image

Such an approach makes it possible to implement simple linear logic with virtually no dependencies and is quickly modified. The set of keys in the parameters of the reassembly function is the ease of perception of the functional and its modification.

In this case, all Volvo models are selected as follows, where the brand identifier is obtained from the get page request:

<? pre (rb ("models", "marks_id", "id", $ _GET ['marks_id']))?>

 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 choose under other fields. Sample model by its id is as follows:

<? pre (rb ("models", "id", $ _GET ['id']))?> where $ _GET ['id'] = 2;

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

Choose one divided each brand:

<? pre (rb ("models", "marks_id"))?>

 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) ) 

Similarly, the treatment is made from the model to the brand of car.

 <? if($models = rb("models", "id", $_GET['id'])): ?> <? pre(rb("marks", "id", $models['marks_id'])); ?> <? else: pre("  "); endif; ?> 

 Array( [1] => Array([id] => 1, [name] => ) ) 

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

Well, actually the implementation of this function .

Source: https://habr.com/ru/post/279029/


All Articles