Zend_Gdata and Zend_View_Helper_Partial

1 message Options
Embed this post
Permalink
Bradley Holt

Zend_Gdata and Zend_View_Helper_Partial

Reply Threaded More More options
Print post
Permalink
I was recently attempting to iterate over a collection of Zend_Gdata_Calendar_EventEntry objects and use them as the model with the new Zend_View_Helper_PartialLoop class. The iteration worked fine, but on each iteration Zend_View_Helper_Partial was called with a Zend_Gdata_Calendar_EventEntry object. Zend_View_Helper_Partial expects a model object that either implements toArray or that can have its properties extracted with get_object_vars. Since Zend_View_Helper_Partial doesn't implement toArray and it has no object vars (since it uses getters) I would end up with no data in my view script.

In my controller:

$eventFeed = $calendar->getCalendarEventFeed($query);
$this->view->assign('eventFeed', $eventFeed);

In my layout view script:

<?= $this->partialLoop('event.phtml', $this->eventFeed) ?>

Zend_View_Helper_PartialLoop then iterates through the event feed passing each Zend_Gdata_Calendar_EventEntry to Zend_View_Helper_Partial::partial() which does the following with it ($model in the code snippet below):

if (!empty($model)) {
    if (is_array($model)) {
        $view->assign($model);
    } elseif (is_object($model)) {
        if (method_exists($model, 'toArray')) {
            $view->assign($model->toArray());
        } else {
            $view->assign(get_object_vars($model));
        }
    }
}

As you can see based on that code snippet, the Zend Framework MVC component is expecting models to either implement the toArray function or have properties that can be extracted through get_object_vars. Based on this, I would think that any components in the Zend Framework that could be used as models should implement toArray (unless their properties can be extracted through get_object_vars).

I went ahead and implemented toArray on selected Zend_Gdata classes in my working copy, as a proof-of-concept, and that worked well. I was able to access the model data in my view scripts. I would send a patch but I have not yet signed a CLA. Basically I implemented the toArray function in the following classes:
  • Zend_Gdata_Calendar_EventEntry
  • Zend_Gdata_Kind_EventEntry
  • Zend_Gdata_App_MediaEntry
  • Zend_Gdata_App_Entry
  • Zend_Gdata_App_FeedEntryParent
The toArray function (that I implemented in my working copy) simply returns an array of the results of all of the classes getters, merging it with the result of its parent classes toArray (with child elements overwriting parent elements) - unless, of course, the class doesn't have a parent that implements toArray. I'm not sure if all the getters need to end up in the array - someone with more knowledge of the internal workings of the Zend_Gdata classes could answer this better. Also, this is just an example for Zend_Gdata_Calendar - this same idea would have to be implemented in all of the Zend_Gdata services so that they could all be used as models with Zend MVC.

Is this something that could be implemented in the Zend_Gdata classes? Or, do you think that this problem should somehow be resolved on the MVC side? Here are links to a case and discussions on this issue:
--
Bradley Holt
[hidden email]