Friday, April 12, 2013

Access Zoo element data anywhere in Joomla easily

Accessing zoo element data is quite difficult as Zoo stores data in JSON format. Normally you have to execute query on zoo_item table & get element column having JSON data, then decode this data & get record details based on Element Id. This is quite boring work. Lets access Zoo element data easily by calling Zoo API. You can use this code anywhere in Joomla.
You can get an element from an item & if you don't have the item, but the item id. Lets get item first using item id as follows.
$app = App::getInstance('zoo');  // Define Zoo app instance
$item = $app->table->item->get($ITEM_ID); // Zoo Record Id 
We have item now, lets get element. 
$Element_Id = "2c0332af-7c7b-4c85-82f7-4d4b7ff5b030"; // One of the text field element id
$element_value = $item->getElement($Element_Id)->getElementData()->get('value');

// $element_value contains the actual value of element id $Element_Id of item id $ITEM_ID.

I always wonder how flexible this way is as you should know the id of the element & how does zoo stores JSON data for that element.I have to write following line, to get the image path of the record.
$Img_Path = $item->getElement('1e50c141-9d05-4c8d-9c7e-2ce7d07e799f')->getElementData()->get('file');
However this method is completely useless for multiple values of each field. It gives you only first value of the element. You can refer zoo documentation of accessing zoo element here.


4 comments: