this is one possible approach: http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
On 17 Apr., 05:54, cricket <[email protected]> wrote: > On Sat, Apr 16, 2011 at 5:34 PM, turbo2ltr <[email protected]> wrote: > > So I'm brand new to cakePHP, but I consider myself a pretty decent php > > developer. I'm trying to give it time to soak in, but at this point > > it would have been faster to write php. Hopefully someone can shove > > me in the right direction as I'd really like to learn this framework. > > I got my start by going through all of Andrew Perkins' video tutorials > > on youtube, which really give me a decent base to start from. > > > I've spent several hours on this, reading, searching and trying and I > > can't seem to find a good answer. > > > I have a table "Parts" and one of the fields is "Type". Normally I'd > > just declare this as an enum with the values as "Part", "Assembly" and > > "Reference". > > > I'm having a hell of a time trying to get cake to output a select tag > > with this data. > > > While I found some workarounds in various blogs, I read that cake > > doesn't really know what to do with the enum type yet, > > > I was able to manually create an array of types and pass it to the > > $form->select in the view which created the proper select tag, but > > hard coding the types might paint me into a corner down the road, and > > I couldn't seem to get the select to pre-select the current value in > > the edit action. > > > So I decided to try an association. I created a table called > > part_types and related it to parts. I changed the type field to > > part_type_id. This relation worked (after a lot of trial and > > error...it was my first association), but did little to help me to > > automatically create the select tag. > > > So I guess my question is, whats the standard way to do this? It > > seems so simple. > > You're on the right track. Cake does not use ENUMs and the workaround > is to use an association (which is better from a normalised DB > standpoint, anyhow). > > Your part_types table should have columns id (PK, auto_increment) and > name. If the column iscalled something other than name set the > $displayName class var in Part. > > In your controller: > > public function admin_add() > { > if (!empty($this->data)) > { > $this->Part->set($this->data); > > if ($this->Part->validates()) > { > if ($this->Part->save()) > { > // redirect > } > else > { > // flash msg -- save failed > } > } > else > { > // flash msg -- error in submission > } > } > > // will run on first invocation or if validation/save fails > $this->set( > 'part_types', > $this->Part->PartType->find('list') > ); > > } > > view: > echo $this->Form->select('Part.part_type_id', $part_types); > > It's important where you put the find('list') line. On first > invocation the if block will be bypassed. When the form is submitted, > you do your validation, then save. If either of those fails, set the > flash msg but do not redirect. That way, the find is run again. > Because $this->data is set this time, it's passed to the view, meaning > that the form will be pre-populated with whatever the user entered the > first time. Not really part of your question I know. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php
