Foreach (element)

From JRapid

Revision as of 18:43, 27 May 2011 by Christian (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

foreach is an element, child of property.



The foreach element is useful when you have an entity A which specifies an embedded inline collection property of an entity B and you want that collection to be automatically loaded with elements of an entity C.

A first approach to solve this requirement will be overriding the find() method of the entity A. The new find() method can query all the C elements and then for each element of the entity C, create a new element B to hold that element. Finally, return the entity A with the collection of B elements.

JRapid's] foreach does the same job. You only need to provide an EL expression for querying all the C elements and specify which is the property of the B entity which will hold each element.

The foreach element is specified inside the embedded inline collection property.


  • select: EL expression for getting the collection of elements to iterate.
  • setproperty: the property of the embedded inline entity for storing the items of the collection returned by the select expression.
  • property: only applicable when the foreach is inside a defaultset. It indicates the properties where it will be applied.


   ... >


  • Loading a company's additional data fields:
"foreach" example
<!-- Entity A -->
<entity label="Company" name="Company">
   <property childproperty="company" collection="sortedset" embedded="inline"
      entity="DataValue" label="Additional Info" labelposition="top" name="additionalData"
      <foreach select="Data:findAll()" setproperty="data"/>

<!-- Entity B -->
<entity label="AdditionalInfoRow" name="AdditionalInfoRow">
   <property entity="Company" label="Empresa" name="company"/> //Connects entity A with B
   <property display="primary" entity="Data" label="Data" name="data"/> //Connects entity B with C
   <property display="secondary" label="Value" name="value"/>

<!-- Entity C -->
<entity label="Data" name="Data">
   <property display="primary" label="Data" name="data"/>

Generated code will populate the additionalData collection with one instance of DataValue entity for each instance of Data entity retrieved by the select expression.

  • DailyCurrenyRates example:
"foreach" example
    <entity label="Currency" name="Currency">
        <property display="primary" label="Name" name="name"/>

    <entity label="CurrencyValue" name="CurrencyValue">
        <property entity="Currency" fixed="fixed" label="Currency"
        <property label="Value" name="value" type="double"/>
        <property entity="DailyCurrencyRates" label="Daily Currency Rates"

    <entity label="Daily Currency Rates" name="DailyCurrencyRates">
        <property default="now" label="Date" name="rateDate" type="date"/>
        <property childproperty="dailyCurrencyRates" collection="sortedset"
         embedded="inline" entity="CurrencyValue" label="Values" 
         labelposition="fieldset" name="currencyValues" setorder="currency">
            <foreach select="Currency:findAll()" setproperty="currency"/>

See also

Personal tools