ZF-11827: Zend_View_Helper_PartialLoop throws exception when model keys start with underscore
Description
I'm using partialLoop with array that originates from MongoDB, so its structure is as follows:
$model = array(
0 => array('_id' => '4e9d6f2dd434488c1baf024a', 'otherKey' => 'otherData'),
1 => array('_id' => '4e9d6f2dd434488c1baf0246', 'key' => 'data'), // And so on
);
When I call
$this->partialLoop('mypartial.phtml, $model);
I get an exception {quote} Zend_View_Exception: Setting private or protected class members is not allowed in /library/Zend/View/Abstract.php on line 815 {quote} I found out that this is because of underscores in array keys (i.e. keys '_id' which are natural for MongoDB data). I also tried it with other keys with underscores (just to make sure '_id' doesn't conflict with some inherited field or something), like '_dummyKey', and it didn't work either. Perhaps this issue is not very serious, but it makes working with MongoDB data quite tedious.
Comments
Posted by Chris Paterson (cjrpaterson) on 2012-02-17T13:20:46.000+0000
This is especially annoying given the fact that you can't opt to not return the _id field in a Mongo query, so the only solution is to loop over the result and unset this field. Tedious indeed!
Posted by Daniel.thomas (danielt) on 2013-01-06T00:48:22.000+0000
Seconding this issue. It is quite frustrating (although work aroundable), particularly for someone brand new to working with ZF.
There are 3 solutions I can think of to get the correct behaviour. 1) Use Reflection to check if the variable is indeed private/protected (ignore the _ prefix) (may have some performance impact, will still object to actual conflicts between user variables and private/protected variables).
2) use Magic Getter/Setter functions to move user supplied variables to a seperate name space (e.g. array etc) (no idea what the impact of this would be)
3) prevent direct assignment ($view->var) require use of $view->assign(), and set user variables into an array or simmilar for retreival with another function. (this will break a lot of peoples scripts).