3.1.2 Collections and Entities


What is a Collection or Entity?

Collections and Entities are used to get data out of a controller (Example: Blog posts from a blog controller). A collection gives you one or more entities as an array of data.

To get blog posts from a controller, a collection is used, this can be seen below.

*CODE* *SNIPPIT* {* Get blog post list *}{collection assign=items controller=$controller pageSize=$pageSize}*CODE*

It is also possible to call an entity directly to get data (Example: A blog post, as seen below).

*CODE* *SNIPPIT*{* Get blog item *}{entity controller=$controller assign=item}*CODE*

If you would lie to read about a controller, you can use the code below (this opens up in a popup window).

*CODE* *SNIPPIT* {help doc=blogController}*CODE*

Can I change data?

Collections and Entities are intitialised automatically with a standard row parameter. This can be overidden if you would like to. For a blog:

  • id - An id for blog posts.
  • pageId - An id for blog pages.
  • languageIso - ISO language code.
  • categoryId - An id for blog categories.
  • year - Year of a blog post.
  • month - Month of a blog post.
  • search - Seach for a blog post.
  • hasPublicering - Whether or not to take account of a release.
  • pagSize - Size of a blog post.

How can I get data out?

When a collection is set, it is possible to get data out (See Code Below).

*CODE* *SNIPPIT*{* Loop through blog articles *}{foreach $items->getData() as $item}Titel: {$item->Title}{/foreach}*CODE*

It is also possible to check the seize of an array (Shown Code Below).

*CODE* *SNIPPIT*{* Calculate the actual size of the collection *}{$collectionSize = $items->getActualSize()}{if $collectionSize gt 0}Der er data.{else}Der er ingen data{/if}*CODE*

Finally, it is possible to dump all data within an array (See Code Below).

*CODE* *SNIPPIT*{$items|var_dump}*CODE*

So it is possible to print and manipulate data.