{"id":6448,"date":"2014-04-18T05:38:24","date_gmt":"2014-04-18T05:38:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/zend-framework-partialloop-with-associative-array-of-models-collection-of-common-programming-errors-2\/"},"modified":"2022-08-30T15:47:09","modified_gmt":"2022-08-30T15:47:09","slug":"zend-framework-partialloop-with-associative-array-of-models-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/zend-framework-partialloop-with-associative-array-of-models-collection-of-common-programming-errors-2\/","title":{"rendered":"Zend Framework partialLoop with associative array of models-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m making a web application in Zend Framework. I&#8217;ve reached the stage of <em>cleaning up<\/em>. As things often go, I have a couple of messy view scripts that have become utterly unreadable (tons of <code>(v)sprintf<\/code>&#8216;s and loops).<\/p>\n<p>There&#8217;s one view that&#8217;s an absolute nightmare&#8230; (no\/inaccurate comments, shorthand&#8230; all things considered to be mortal sins). Just an example:<\/p>\n<pre><code>$rows[$c] .= ''.sprintf('',$t.'['.$ref-&gt;getCode().']').str_replace('&gt;'.$ref-&gt;getCValue().''.$ref-&gt;getCValue().'partialLoop('controller\/_myPartial.phtml',array('model'=&gt;$arr));\n\/\/in the partial loop:\nZend_Debug::dump($this-&gt;m);\n<\/code><\/pre>\n<p>I see all my models correctly, but their keys have all been turned into properties.<\/p>\n<pre><code>$this-&gt;FOO-&gt;someMethod();\/\/works fine\n<\/code><\/pre>\n<p>Bur I want it to be:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>I&#8217;ve also tried using <code>$this-&gt;partialLoop()-&gt;setObjectKey('Mylib_Model_Person');<\/code>, but that didn&#8217;t seem to make any difference, other then confuse me.<\/p>\n<p>The only solutions I see is either <code>array_map<\/code>, but that would defeat the point (I&#8217;m trying to end up with a clean view script); or rewrite a part of my service layer, to return the data ready structured, and keep the <code>array_map<\/code> there.<\/p>\n<p>I can&#8217;t help thinking that what I want to do, essentially use a partialLoop as an <code>array_map<\/code> callback, is possible. If it isn&#8217;t, is there an alternative? Any thoughts?<\/p>\n<p>I&#8217;ve tried <code>get_object_properties($this)<\/code>, and iterate through the object properties, to no avail, the loop simply doesn&#8217;t get executed(?!)<\/p>\n<p>As it turns out <code>$this-&gt;partialLoop()-&gt;setObjectKey('Mylib_Model_Person');<\/code> should have been <code>$this-&gt;partialLoop()-&gt;setObjectKey('model');<\/code>. If I do change this, and start the partial loop by dumping <code>$this-&gt;model<\/code>, I see my model. However:<\/p>\n<pre><code>echo $this-&gt;model-&gt;someMethod(); \/\/throws error: method on non-object\nZend_Debug::dump(get_class_methods($this-&gt;model));\/\/shows all methods, including someMethod()\n<\/code><\/pre>\n<p>And to add insult to injury, tears and confusion. The model implements the toArray-thing, so I tried:<\/p>\n<pre><code>echo $this-&gt;model['someData'];\/\/Error: cannot use object of type Mylib_Model_Person as array!!\n<\/code><\/pre>\n<p>So, it&#8217;s not an object when I try to use methods, it&#8217;s an object when trying to access data as an array, and when using the magic getter method (<code>$this-&gt;model-&gt;some_Data<\/code>) it doesn&#8217;t do anything. No errors, but no output either. The view is rendered as is.<\/p>\n<p>I&#8217;m thinking I ran into a bug. I&#8217;ll rapport it. Consider this:<\/p>\n<pre><code>$methods = get_class_methods($this-&gt;model);\nwhile($m = array_shift($methods))\n{\n    if (substr($m,0,3) === 'get')\n    {\n        Zend_Debug::dump($m);\/\/e.g getName\n        Zend_Debug::dump($this-&gt;model-&gt;{$m}());\/\/'Foobar'\n        $m = 'someMethod';\/\/copy-paste, so typo's aren't to blame\n        Zend_Debug::dump($this-&gt;model-&gt;{$m}());\/\/'the data I was after'\n    }\n}\n<\/code><\/pre>\n<p>So that works, but the, if I try:<\/p>\n<pre><code>$this-&gt;model-&gt;{'someMethod'}();\/\/Error again\n\/\/or even:\n$m = 'someMethod';\necho $this-&gt;model-&gt;{$m}();\/\/Error...\n<\/code><\/pre>\n<p>That can&#8217;t be right<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m making a web application in Zend Framework. I&#8217;ve reached the stage of cleaning up. As things often go, I have a couple of messy view scripts that have become utterly unreadable (tons of (v)sprintf&#8216;s and loops). There&#8217;s one view that&#8217;s an absolute nightmare&#8230; (no\/inaccurate comments, shorthand&#8230; all things considered to be mortal sins). Just [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"class_list":["post-6448","post","type-post","status-publish","format-standard","hentry","category-framework","category-zend-framework"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6448"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6448\/revisions"}],"predecessor-version":[{"id":8876,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6448\/revisions\/8876"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}