logo

MMBase tips

Properties on nodes

tip #20 / Thu 22 Sep 2011 / André / 0 comments

Properties are a simple way of adding extra values to nodes, often as configuration sometimes when you are short of fields. The default mmbase property implementation can contain key/value pairs.

Properties themselves are nodes of type 'properties' and like aliasses save a reference to the node they apply to. A properties nodes has a field 'parent' (their node), a field 'name' and field 'value'. Properties are typically used as configuration, by default key and value keep just 255 characters.

If the MyNews magazine node with alias 'default.mags' would contain properties you could list them as follows. To see some examples you can add them in my_editors at the bottom of the edit node form.

<mm:node number="default.mags"> 
  <mm:hasfunction name="list" nodemanager="properties">
     <mm:nodelistfunction nodemanager="properties" name="list" referids="_node@node">
       key <mm:field name="key" /> 
       value <mm:field name="value" />
     </mm:nodelistfunction>
  </mm:hasfunction>
</mm:node>

To see an examples you can add a property in my_editors at the bottom of the edit node form.

Most trafic is done with functions. Looking up a specific property for a node, in this example 'oai-set', is done with 'get'.

<mm:node number="default.mags"> 
  <mm:import id="oaikey">oai-set</mm:import>
  <mm:function referids="_node@node,oaikey@key" nodemanager="properties" name="get" />
</mm:node>

That will print a properties' value.

Setting properties

A property can only exist once on a node. When you save one with the same key on the same node, it's previous value gets overwritten. Saving a node is done with 'set'.

<mm:node number="default.mags"> 
  <mm:import id="property_key">oai-set</mm:import>
  <mm:import id="property_value">A new value</mm:import>
<mm:function referids="_node@node,property_key@key,property_value@value" nodemanager="properties" name="set" write="false" /> </mm:node>

Listing properties

Properties are normal nodes, you can still for example list all properties with a specific key.

<mm:listnodescontainer type="properties">
  <mm:constraint field="key" value="oai-set" operator="EQUAL" />
  <mm:listnodes>
    node <mm:field name="parent" />
    key <mm:field name="key" />
    value <mm:field name="value" />
  </mm:listnodes>
</mm:listnodescontainer>

This prints all key/value pairs and the number they belong to where the key is equal to 'oai-set'.

0 comments

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm