Collections and Entities

In this article:


    What is a collection or entity?

    A collection or entity is used to retrieve data from a controller, for example a blog-post from a blog-controller. A collection provides you with one or more entities, that is a range of data.

    To retrieve blog-posts from a blog-controller, a collection is used, as shown below.

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

    It is also possible to call an entity directly to retrieve e.g. one blog post, as shown below.

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

    If you want to read about a controller, you can use the code below(opens in a pop-up window).

    
    {help doc=blogController}
    
    

    Can I change the data?

    A collection and an entity is initializered automatically with a range of standard parameters. These can be overwritten, if needed. For example for blog:

    • id - an id for a blog-post
    • pageId - an id for the for the blog-post
    • languageIso - ISO language code
    • categoryId - an id for the blog-category
    • year - the year the blog-post was published
    • month - the month the blog-post was published
    • search - search for the blog-post
    • hasPublicering - whether publishing should be taken into account
    • pageSize - number of blog-posts

    Note on encoding and special characters in collections
    If you use special characters like æøå etc., you can either:



    Screenshot of admin

    1. Use html-entities:
    2. Save the file as ISO-8859-1:
    3. Save the file as UTF-8:

    How do I retrieve data?

    Once a collection is set, it is possible to retrieve data, as shown below.
     

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

    It is also possible to check the size of the array, as shown below:
     

    
    {*** Calculate the actual size of the collection ***}
    
    {$collectionSize = $items->getActualSize()}
    
    {if $collectionSize gt 0}
    
    There is data.
    
    {else}
    
     There is no data.
    
    {/if}
    
    

    Finally, you can print all the data in the array, as shown below:

    
    {$items|var_dump}
    
    

    It is then possible to print and manipulate data. This is elaborated on under the tab data