Showing posts with label Access zoo records. Show all posts
Showing posts with label Access zoo records. Show all posts

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.


Friday, October 26, 2012

How to render zoo records easily

Zoo - one of the most popular Joomla extension has a view which shows just a single record, that is of course full view. Zoo doesn't have a view which shows a list of records of particular submission type. So what if you want to show a list of zoo items of particular type in your extension.
You must be thinking what's the big deal, based on submission id just fetch the records & show it how you want. Wait a minute. Its not as easy as you think as Zoo uses JSON method to store record's which is quite difficult to render zoo elements. So its quite complicated to get zoo items details from database by executing queries.
Even if you manage it won't be robust cause to get related records from table you have to deal with hard coded 36 key string, which zoo uses as field identifier. So lets get away from this hassle.
Here is the snippet to render the details of zoo records quite easily.

$zapp = App::getInstance('zoo');
$items = $app->table->item->all(array('conditions' => 'id = '.$Zoo_Item_id));
//$Zoo_Item_id must be zoo item id of which you want to render details

foreach($items as $item)
{       
   foreach ($item->getElements() as $id => $element)
   {
      echo $element->render();
} }
Here you can see how we are showing Zoo related records in table. We have added link for full view of respective Zoo Item as well.