Modeling MySQL enum

7 messages Options
Embed this post
Permalink
umpirsky

Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
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ć.
keith Pope-4

Re: Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
http://www.whitewashing.de/blog/articles/120

2009/9/22 umpirsky <[hidden email]>:

>
> 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-tp25530643p25530643.html
> Sent from the Zend DB mailing list archive at Nabble.com.
>
>



--
----------------------------------------------------------------------
[MuTe]
----------------------------------------------------------------------
umpirsky

Re: Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
Interesting approach, thx for the link,

What do you think about adding method to MyEnum class for getting array of all possible values (all constants)?

Regards,
Saša Stamenković


On Tue, Sep 22, 2009 at 11:29 AM, keith Pope <[hidden email]> wrote:
http://www.whitewashing.de/blog/articles/120

2009/9/22 umpirsky <[hidden email]>:
>
> 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-tp25530643p25530643.html
> Sent from the Zend DB mailing list archive at Nabble.com.
>
>



--
----------------------------------------------------------------------
[MuTe]
----------------------------------------------------------------------

Mert Oztekin

RE: Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
In reply to this post by keith Pope-4
Some javascript/style in this post has been disabled (why?)
Good article
 
-----Original Message-----
From: keith Pope [[hidden email]]
Sent: Tuesday, September 22, 2009 12:30 PM
To: umpirsky
Cc: [hidden email]
Subject: Re: [fw-db] Modeling MySQL enum
 
 
2009/9/22 umpirsky <[hidden email]>:
>
> 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ć.
> --
> Sent from the Zend DB mailing list archive at Nabble.com.
>
>
 
 
 
--
----------------------------------------------------------------------
[MuTe]
----------------------------------------------------------------------
 

  ________________________________  
Bu mesaj ve ekleri, mesajda gönderildiği belirtilen kişi/kişilere özeldir ve gizlidir. Size yanlışlıkla ulaşmışsa lütfen gönderen kisiyi bilgilendiriniz ve mesajı sisteminizden siliniz. Mesaj ve eklerinin içeriği ile ilgili olarak şirketimizin herhangi bir hukuki sorumluluğu bulunmamaktadır. Şirketimiz mesajın ve bilgilerinin size değişikliğe uğrayarak veya geç ulaşmasından, bütünlüğünün ve gizliliğinin korunamamasından, virüs içermesinden ve bilgisayar sisteminize verebileceği herhangi bir zarardan sorumlu tutulamaz.

This message and attachments are confidential and intended for the individual(s) stated in this message. If you received this message in error, please immediately notify the sender and delete it from your system. Our company has no legal responsibility for the contents of the message and its attachments. Our company shall have no liability for any changes or late receiving, loss of integrity and confidentiality, viruses and any damages caused in anyway to your computer system.
ajmurray

Re: Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
In reply to this post by umpirsky

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.

umpirsky

Re: Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
In reply to this post by umpirsky
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 <[hidden email]> 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.


ajmurray

Re: Modeling MySQL enum

Reply Threaded More More options
Print post
Permalink
In reply to this post by umpirsky
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ć.