2009年9月23日星期三

Re: [fw-db] Modeling MySQL enum

Thats nice, but problem with this approach is when you want to use enum like constant:

Enum::MONDAY

in your case it can be:

Enum::days[0]

if you know what I mean.

Regards,
Saša Stamenković


On Thu, Sep 24, 2009 at 5:48 AM, netlynx <ajmurray01@gmail.com> wrote:

My way of handling mysql enums would be something like:

class Default_Model_Article {

   public static $state = array('published', 'unpublished',
'pending_review','incomplete');

}


Then it is usable from anywhere without actually instansiating the class.
ie from the view:

.... [snippet from form] ...
<select name="state">
   <?php foreach (Default_Model_Article::$state as $stateOption): ?>
   <option><?php echo $this->escape($stateOption); ?></option>
   <?php endforeach; ?>
</select>

... [maybe for validation] ...
if (!in_array($articleState, Default_Model_Article::$state)) {
   // Sorry you have made an invalid selection
}

It just seems like a flexible way of handling it.  (and on a side note, I
might be mistaken on this, but I don't think you could use arrays in a
constant context).  Anyhow, that would be how I would tie it into my model
and handle it.

Peace
Aaron



umpirsky wrote:
>
> Hi.
>
> I wonder what is the best way to model MySQL enums? Class with constants,
> or array of constants, or maybe sth else? You need to render options in
> templates (<select>) or to use enum values allll over the code.
>
> So, what is the right way to go?
>
> Regards,
> Saša Stamenković.
>

--
View this message in context: http://www.nabble.com/Modeling-MySQL-enum-tp25530643p25531375.html
Sent from the Zend DB mailing list archive at Nabble.com.


没有评论: