Comments on “CakePHP: Watch Out for the compact() Function” http://robwilkerson.org/2008/12/28/cakephp-watch-out-for-the-compact-function/feed 2009-06-18T16:52:26-04:00 Chyrp CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-18:/id/160//comment_9517 2009-06-18T16:52:26-04:00 2009-06-18T16:52:26-04:00 Rob Wilkerson http://robwilkerson.org <p>jesse: I’m conflicted and admittedly inconsistent on this issue. Usually, when writing in case sensitive languages, I’ll use <code>lower_with_underscores</code> and <code>headlessCamelCase</code> with case insensitive languages, but especially with variable names (as opposed to class/method names), I’m not as consistent as I really should be. I kind of like both and tend to fluctuate far too much. :-)</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-18:/id/160//comment_9516 2009-06-18T16:49:54-04:00 2009-06-18T16:49:54-04:00 Rob Wilkerson http://robwilkerson.org <p>That’s awesome, Nate. Thanks for the heads up. </p> <p>Like most things it’s not such a big deal as long as you know to expect it, but I do prefer that my tools not modify entities that they are explicitly asking me to set. This is a decision that I’m glad to see reversed (even if it may cause me some short-term upgrade pain since I may have tweaked my view instead of my controller).</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-18:/id/160//comment_9502 2009-06-18T10:37:08-04:00 2009-06-18T10:37:08-04:00 Nate Abele http://cakephp.org/ <p>Hey Rob,</p> <p>Yeah, I always thought that was kind of a dumb “feature”, so it’s been reverted in 1.3. Your variables will now always show up exactly as you set them, regardless of how you pass your parameters to set().</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-18:/id/160//comment_9501 2009-06-18T09:21:14-04:00 2009-06-18T09:21:14-04:00 Jesse http://www.techno-geeks.org <p>Hump notation for the win! After switching to hump/camel notation for a couple years I will never go back to the underscore format! I can’t say I have ever ran into this simply because I always use hump notation for my variables in Cake.</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-16:/id/160//comment_9426 2009-06-16T10:32:47-04:00 2009-06-16T10:32:47-04:00 Jeff Keen http://www.lesson-share.com <p>I apologize. I did that from memory and it was wrong. The second param is for the “set” method… and you want to use “false” rather than true. No detail of this in the CakePHP <span class="caps">API</span>, but it goes like this:</p> <p>$this->set(compact(‘foo’, ‘bar’, ‘vendor_type’), false);</p> <p>Taylor Luk pointed it out in a comment on the following blog: <a href="http://myeasyscripts.com/loudbaking/beware-of-the-underscore/#comments">http://myeasyscripts.com/loudbaking/beware-of-the-underscore/#comments</a></p> <p>This was brought up in Google Groups: <a href="http://groups.google.com/group/cake-php/browse_thread/thread/a136b91f49f1befb?pli=1">http://groups.google.com/group/cake-php/browse_thread/thread/a136b91f49f1befb?pli=1</a>. It appears that it will be resolved in version 1.3. Hope this helps.</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-15:/id/160//comment_9400 2009-06-15T17:39:43-04:00 2009-06-15T17:39:43-04:00 Rob Wilkerson http://robwilkerson.org <p>Hey, Jeff – would you mind citing your source for that info? It sounds great, but I can’t find any indication of that second parameter in either <span class="caps">PHP</span>’s <code>compact()</code> function or in CakePHP’s <code>Controller::set()</code> method.</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2009-06-15:/id/160//comment_9397 2009-06-15T16:39:58-04:00 2009-06-15T16:39:58-04:00 jeff keen http://www.lesson-share.com <p>compact() takes another variable that solves the underscore problem.</p> <p>$this->set( compact(‘vendor_type’), true);</p> <p>Include “true” when using variables with underscores, and then they will show up in your view as is.</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-30:/id/160//comment_4147 2008-12-30T03:39:03-04:00 2008-12-30T03:39:03-04:00 JIESCH <p>$this->set (‘data’ , $res=compact ( ‘vendor_type’, ‘application_type’, ‘states’ ) );</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-29:/id/160//comment_4106 2008-12-29T18:36:56-04:00 2008-12-29T18:36:56-04:00 Rob Wilkerson http://robwilkerson.org <p>Fair enough, ADmad. And quite true. I’ll take a look and try to figure out the best way to do so this week. Thanks for the, um, encouragement. :-)</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-29:/id/160//comment_4096 2008-12-29T16:14:02-04:00 2008-12-29T16:14:02-04:00 ADmad <p>Anyone can submit updates to the cookbook. And that anyone can be you :)</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-29:/id/160//comment_4076 2008-12-29T11:37:06-04:00 2008-12-29T11:37:06-04:00 dr. Hannibal Lecter http://dsi.vozibrale.com/ <p>That’s strange.. If this is a cake thing, how come it doesn’t happen when you’re not using compact? This is completely absurd. Same method, different output results? Why? If I set my variable as $mega_crap I want $mega_crap and not some other var name.</p> <p>Weird…</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-29:/id/160//comment_4071 2008-12-29T09:21:00-04:00 2008-12-29T09:21:00-04:00 Rob Wilkerson http://robwilkerson.org <p>Thanks for the info, all. I agree that it’s not inconsistent with CakePHP’s conventions, but I do wish its use was documented (or documented more prominently). Renaming variables that were explicitly set is bound to cause confusion among those that, like me, are new to CakePHP. </p> <p>I’d probably go one step further and argue that it’s not a good practice, but if it’s well documented then at least it won’t be a surprise.</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-29:/id/160//comment_4070 2008-12-29T08:14:31-04:00 2008-12-29T08:14:31-04:00 Ferdinant http://www.vtek.nl <p>It’s indeed Cakes Naming Convention that takes care of this. They might take a note on this one at the cakephp book, if it comes to me. :)</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-28:/id/160//comment_4059 2008-12-28T23:47:23-04:00 2008-12-28T23:47:23-04:00 Mike http://www.mikebernat.com <p>Good catch.. I’m sure I’ll get bitten by that sometime. It definitely follows the same sort of conventions cakephp normally enforces so I would have to agree with Adam that it’s not a compact() thing. </p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-28:/id/160//comment_4054 2008-12-28T22:47:03-04:00 2008-12-28T22:47:03-04:00 Brian d. http://realm3.com <p>I’ve been bitten by this same problem before and now I just use $this->set(‘var’, $var) most of the time instead of using compact().</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-28:/id/160//comment_4046 2008-12-28T20:55:19-04:00 2008-12-28T20:55:19-04:00 Adam Royle <p>It’s changed by cakephp – nothing to do with the compact() function. Personally I don’t agree with cakephp renaming it without a warning, but I guess they want to standardise variable names.</p> CakePHP: Watch Out for the compact() Function tag:robwilkerson.org,2008-12-28:/id/160//comment_4039 2008-12-28T19:21:54-04:00 2008-12-28T19:21:54-04:00 ADmad <p>Its done by CakePHP :) Cake’s convention is that view variable names should be in camelBacked form.</p>