Path

ez.no / ezpublish / documentation / customization / custom design / database connectivity


Database connectivity

These documentation pages are no longer maintained. Please visit the new documentation site.

Fetch from database

If we wish to get something from the database we usually use a template function called fetch. fetch takes three arguments: name of the module we fetch from, what we wish to fetch (the fetch function) and, optionally, a set of parameters.

The set of parameters are sent as an associative array. Associative arrays are created with the hash function.

Example:

hash( 'First name', 'Jo Henrik', 'Last name', 'Endrerud' )

This would create an array with two values. The first key is First name with the value Jo Henrik, the second key is Last name with value Endrerud.

It doesn't matter if you use single or double quotes, but it is a good practise to use double in your HTML and single in your eZ publish code.

We usually wish to fetch information on the content of the nodes. The only module we will explain here is therefore the content module.

Fetch one node

To fetch one node we use the fetch function node. This function take one parameter, node_id, that defines the ID of the node we wish to fetch.

Example:

{let mynode=fetch( 'content', 'node', hash( 'node_id', 5 ) )}

This would fetch the node with ID 5 from the database and store it in the variable mynode. We can then access the $mynode object and get the information we want (for example content from the data map).

Fetch many objects

If we wish to make a list of all nodes placed under another node (i.e. a folder) in the tree, we use a fetch function called list. This function takes many parameters, most of them are optional. The one required parameter is called parent_node_id and it describes what parent we should fetch the nodes from.

Example:

{let elements=fetch( 'content', 'list', hash( 'parent_node_id', 2 ) )}

Our variable elements will here be an array containing all nodes placed under node 2. list have several other parameters to customize the way the nodes are returned.

Parameter

Description

parent_node_id

Parent placement of the nodes we wish to fetch

sort_by

How we should sort the nodes

limit

How many nodes we maximum should fetch

offset

The position we should start the node if we don't want every node

class_filter_array

An array of classes if we only wish to include some classes

class_filter_type

Type of filtering, could be include or exclude. Either only use the classes declared in class_filter_array or include every class except the ones declared

More fetch functions

eZ systems has made several more fetch functions for you. This is functions for counting number of nodes under another node, fetching an entire tree from a specific node and so on. You can check the file kernel/content/function_definition.php for more information about the functions and parameters you can use.

Comments

FYI: Grammatical Correction

Just because I thought it might be helpful:

[quote]This is functions for counting number of nodes under another node, fetching an entire tree from a specific node and so on[/quote]

It would be closer to "proper" English to say:

There are functions for: 1) counting the number of nodes under another node, 2) fetching an entire tree from a specific node, 3) and so on.

Well, that's not really *that* much more grammatically correct. But it probably looks more correct to a Native English speaker. A "grammatical" version might look like:

There are functions for both counting the number of nodes under another node and fetching an entire tree from a specific node, among others.

I'm not sure how readable the "grammatically correct" version is, though. English is *such* an annoying language. (I say this with authority, since I'm a native speaker).

Contents

Customization

Access control
eZ publish API Documentation
Content structure
Custom design
    User specified parameters
    Template variables set by ezPublish
    Introduction
    Variables, arrays and objects
    Node
    Database connectivity
    Sections
    File placement
    Override templates
    node_view_gui
    Pagination
    Caching
    Stylesheets
    Pagelayout
    Printable pages
    Examples
Components
Tips & Tricks
Troubleshooting


Created

04/07/2003
11:27:32 am
by Bård Farstad

Last updated

08/07/2003
1:14:42 pm
by Bård Farstad

Authors

Bård Farstad



This page is part of the eZ Publish documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.