Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
examples:builder:hasmany_relation [2021/03/27 14:47] pdsci-admin [Step 5: Create the config_relation.yaml controller file] |
examples:builder:hasmany_relation [2021/04/11 19:06] (current) pdsci-admin [Step 4: Announce the relation to the parent model] |
||
---|---|---|---|
Line 2: | Line 2: | ||
====== The hasMany relation ====== | ====== The hasMany relation ====== | ||
- | | + | |
\\ | \\ | ||
<WRAP tip> | <WRAP tip> | ||
Line 19: | Line 19: | ||
===== What it will look like ===== | ===== What it will look like ===== | ||
+ | This is the parent form //** book **// embedding the related child list // | ||
+ | {{: | ||
+ | <WRAP clear /> | ||
+ | \\ | ||
+ | Additional reviews can be crated with the button //Create Reviews//. By clicking on a row in the reviews table, an existing review can be updated directly. | ||
+ | {{: | ||
+ | <WRAP clear /> | ||
+ | \\ | ||
===== Preparation ===== | ===== Preparation ===== | ||
Prerequisites are\\ | Prerequisites are\\ | ||
Line 40: | Line 48: | ||
* the respective **model** is named **// | * the respective **model** is named **// | ||
* The **controller** for the child is named // | * The **controller** for the child is named // | ||
- | For a //hasMany// relation the child table requires a field named by the parent model trailed by ' | + | For a //hasMany// relation the **child table** requires a field named by the parent model trailed by ' |
- | So the table definitions for te //hasMany// relation looks pretty much the same like the //hasOne// definition. The difference is, that the //has Many// child table can hold more than one record with the same key to the parent record.\\ | + | The table definitions for the //hasMany// relation looks pretty much the same like the //hasOne// definition. The difference is, that the //has Many// child table can hold more than one record with the same key to the parent record.\\ |
+ | |||
+ | Here is what the libraray example reviews table looks like. Notice the last line where the field // | ||
+ | {{: | ||
===== Step 2: Create a model with list and form for the child table ===== | ===== Step 2: Create a model with list and form for the child table ===== | ||
- | Next for the child table (// | + | Next a child table (// |
+ | \\ | ||
+ | This is the form for the reviews: | ||
+ | {{: | ||
+ | \\ | ||
+ | \\ | ||
+ | This is the list view for the reviews: | ||
+ | {{: | ||
+ | \\ | ||
===== Step 3: Extend the parent controller ===== | ===== Step 3: Extend the parent controller ===== | ||
Line 66: | Line 84: | ||
public $implement = [' | public $implement = [' | ||
' | ' | ||
- | ' | + | ' |
| | ||
public $listConfig = ' | public $listConfig = ' | ||
Line 84: | Line 102: | ||
</ | </ | ||
\\ | \\ | ||
- | ===== Step 4: Announce | + | ===== Step 4: Define |
- | To make the parent model able to work with the relation, the type of the relation, the name of the relation and the child model have to be announced | + | To make the parent model able to work with the relation, the type of the relation, the name of the relation and the child model have to be defined |
+ | \\ | ||
+ | {{: | ||
+ | <WRAP clear /> | ||
+ | \\ | ||
The required file is placed in plugins /models/ directory. In case of our example the full path is ' | The required file is placed in plugins /models/ directory. In case of our example the full path is ' | ||
It's only a few lines to add:\\ | It's only a few lines to add:\\ | ||
Line 94: | Line 115: | ||
... | ... | ||
- | | + | |
' | ' | ||
]; | ]; | ||
Line 102: | Line 123: | ||
In line 4 the public variable ' | In line 4 the public variable ' | ||
In line 5 ' | In line 5 ' | ||
+ | By default the key field to look for the records with related IDs will be created by the name of the model trailed by ' | ||
+ | \\ | ||
Remember each definition for a relation is like a table field. A definition can hold several definitions for different relations. Like so:\\ | Remember each definition for a relation is like a table field. A definition can hold several definitions for different relations. Like so:\\ | ||
<sxh php; highlight: [5-9]> | <sxh php; highlight: [5-9]> | ||
Line 170: | Line 193: | ||
label: Reviews | label: Reviews | ||
view: | view: | ||
- | | + | |
toolbarButtons: | toolbarButtons: | ||
manage: | manage: | ||
form: $/ | form: $/ | ||
- | | + | |
... | ... | ||
</ | </ | ||
Line 182: | Line 204: | ||
* The item **label:** holds the text for a label for the field ' | * The item **label:** holds the text for a label for the field ' | ||
/* The **view** segment indicates how the relation will be shown: As a form in our example because there is only one related child record (because of //hasOne//) per parent record.\\ */ | /* The **view** segment indicates how the relation will be shown: As a form in our example because there is only one related child record (because of //hasOne//) per parent record.\\ */ | ||
- | * The item **toolbarButtons** with ' | + | * The item **toolbarButtons** with ' |
* The **manage** sections defines how things are displayed when we are searching or editing an entry | * The **manage** sections defines how things are displayed when we are searching or editing an entry | ||
\\ | \\ | ||
Line 192: | Line 214: | ||
\\ | \\ | ||
\\ | \\ | ||
- | This is the screenshot for the example above:\\ | + | This is the screenshot for the example above - notice that a definition is already set (see the hasOne example):\\ |
- | /* {{: | + | {{: |
<WRAP clear /> | <WRAP clear /> | ||
\\ | \\ | ||
Line 208: | Line 230: | ||
See this screenshot too:\\ | See this screenshot too:\\ | ||
- | /* {{: | + | {{: |
<WRAP clear /> | <WRAP clear /> | ||
\\ | \\ | ||
Line 217: | Line 239: | ||
So we return to Builder > Models > Book > Forms > field.yaml an add a widget //Partial// displaying and managing the created relation.\\ | So we return to Builder > Models > Book > Forms > field.yaml an add a widget //Partial// displaying and managing the created relation.\\ | ||
The field name we have to refer to is // | The field name we have to refer to is // | ||
- | Notice the ' | + | Notice the ' |
- | /* {{: | + | \\ |
+ | {{: | ||
<WRAP clear /> | <WRAP clear /> | ||
\\ | \\ | ||
Now the form is ready to create, show, edit, delete, link and unlink a record from the child table within/to a record of the parent table. | Now the form is ready to create, show, edit, delete, link and unlink a record from the child table within/to a record of the parent table. | ||
---- | ---- |