julian98
Occasional Observer

Get columns of entity in combobox

Jump to solution

Hello,

I would like to access the selected database entity within a CMS_INPUT_COMBOBOX in the template:

<CMS_INPUT_COMBOBOX name="pt_rubrik" hFill="yes" singleLine="no" useLanguages="no">
<CMS_INCLUDE_OPTIONS type="database">
<LABELS>
<LABEL lang="DE">#item.Name_DE</LABEL>
<LABEL lang="*">#item.Name_EN</LABEL>
</LABELS>
<TABLE>Data.rubriken</TABLE>
</CMS_INCLUDE_OPTIONS>
<LANGINFOS>
<LANGINFO lang="DE" label="Rubrik-Icon"/>
<LANGINFO lang="*" label="Category icon"/>
</LANGINFOS>
</CMS_INPUT_COMBOBOX>

The database schema "rubriken" in turn contains another checkbox, which is specified in the related form like this:

<CMS_INPUT_COMBOBOX name="tt_icon" hFill="yes" singleLine="no" sortOrder="ascending" useLanguages="no">
<CMS_INCLUDE_OPTIONS type="database">
<LABELS>
<LABEL lang="*">#item.DisplayName</LABEL>
</LABELS>
<QUERY name="Data.iconsbyrubrik">
<PARAM name="id">0</PARAM>
</QUERY>
<TABLE>Data.icons</TABLE>
</CMS_INCLUDE_OPTIONS>
<LANGINFOS>
<LANGINFO lang="*" label="Icon"/>
</LANGINFOS>
</CMS_INPUT_COMBOBOX>

How can I now access tt_icon within a template? With $CMS_VALUE("pt_rubrik") I only get the ID of the record. But I would like to access other fields (in this case tt_icon). How is this possible?

 

Thanks!

Many Greetings
Julian Scholz

2 Solutions

Accepted Solutions
julian98
Occasional Observer

Solution found: Instead of the form name, the table fields must be accessed directly.

View solution in original post

0 Kudos
hoebbel
Crownpeak employee

Hello Julian,

there are two ways you can get the information. Either via the datasets (recommended) or directly via the entities.
In both cases you have to get the value of the input component first (pt_rubrik.value). This is an entity.
You can make a dataset from it (.getDataset(<name of table template>)). Get its FormData (getFormData) and from it the desired input component (get<language>, "name"). For language-independent input components you can also use <null> as language (otherwise #global.language). From the input component you then need the content (get()).
Since the inner input component is again a combobox with a dataset, it starts again from the beginning. All in all, the whole thing would look something like this:

$CMS_VALUE(pt_rubrik.value.getDataset("Data.rubriken").getFormData().get(null,"tt_icon").get().value.getDataset("Data.icons").getFormData().get(null,"<Name of the DisplayName component>").get())$

Note: Not knowing the project, I guessed some things that are probably not always correct. But I hope that the explanations are enough to find and correct these mistakes 🙂


With the way over the Entites (which is only possible if the input components have been mapped to foreign key relations) looks easier though:

$CMS_VALUE(pt_rubrik.value.<foreignKeyName>.<foreignKeyName>.<DisplayName>)$


But I strongly recommend to go the dataset way - then you are not dependent on which input components are used and how the schema was configured.

Best regards
Holger

View solution in original post

6 Replies
julian98
Occasional Observer

Solution found: Instead of the form name, the table fields must be accessed directly.

0 Kudos
hoebbel
Crownpeak employee

Hello Julian,

there are two ways you can get the information. Either via the datasets (recommended) or directly via the entities.
In both cases you have to get the value of the input component first (pt_rubrik.value). This is an entity.
You can make a dataset from it (.getDataset(<name of table template>)). Get its FormData (getFormData) and from it the desired input component (get<language>, "name"). For language-independent input components you can also use <null> as language (otherwise #global.language). From the input component you then need the content (get()).
Since the inner input component is again a combobox with a dataset, it starts again from the beginning. All in all, the whole thing would look something like this:

$CMS_VALUE(pt_rubrik.value.getDataset("Data.rubriken").getFormData().get(null,"tt_icon").get().value.getDataset("Data.icons").getFormData().get(null,"<Name of the DisplayName component>").get())$

Note: Not knowing the project, I guessed some things that are probably not always correct. But I hope that the explanations are enough to find and correct these mistakes 🙂


With the way over the Entites (which is only possible if the input components have been mapped to foreign key relations) looks easier though:

$CMS_VALUE(pt_rubrik.value.<foreignKeyName>.<foreignKeyName>.<DisplayName>)$


But I strongly recommend to go the dataset way - then you are not dependent on which input components are used and how the schema was configured.

Best regards
Holger

julian98
Occasional Observer

Hello Holger,


thank you very much for your detailed and good explanation!


Best regards
Julian

0 Kudos
julian98
Occasional Observer

 

Hello @hoebbel 

I have now tried the variant with .getDataset and unfortunately realised that our FirstSpirit version does not yet have this method in the Entity class. Is there perhaps an alternative way to access the individual table fields depending on the language?

Many thanks in advance.

Best regards
Julian Scholz

0 Kudos
hoebbel
Crownpeak employee

Hello Julian,

you can either use the the entity and then the names used within the database tables / foreign keys (the not recommended solution in my last answer) or you have to create the datasets from the entity with the use of the content2 node. Unfortunately it is way more complex to get the dataset with an older FirstSpirit version.

First, you have to get all content2 nodes from which datasets are involved. To get them, you can use a syntax like this:

$CMS_SET(myContent2, #global.userService.getStore(class("de.espirit.firstspirit.access.store.Store$Type").CONTENTSTORE,false).getContent2ByName("<Name of the content2 node>"))$


and then you have to convert the entity to the appropriate dataset with this content2 node:

$CMS_SET(set_myDataset,set_myContent2.getDataset(myEntity))$


That is the old way an entity has to be converted to a dataset. Within an up-to-date FirstSpirit version you can use

myEntity.getDataset("<Name of the content2 node>")

instead of the both statements. So you have to replace each methode getDataset with the two statements. If you get an entity as the result of a method, you have to convert it to a dataset with the appropriate content2 node. 

Of course you can reuse all content2 nodes, if they are available within the generation context. So if you get them early within the page template, you can access them in all sections, which are accessed afterwards.

Hint: Maybe you should consider a FirstSpirit update 😉

Best regards
Holger

0 Kudos
julian98
Occasional Observer

Hello Holger,

happy New Year to you and thank you very much for your helpful answer!

Best regards
Julian

0 Kudos