![]() |
|
Snippets |
|
As enum is a MySQL specific type, you cannot have a column of type enum in your schema.yml (which is database-independent). One solution to simulate it is to create another table to store the different possible values. But the Model class offer an alternative solution that is probably more elegant.
Imagine you have a status column in you article table you want to be an enumerated list with values like (open, closed). Simply declare the status column as integer and then modify the model as follows:
In ArticlePeer.php:
class ArticlePeer... { static protected $STATUS_INTEGERS = array('open', 'closed'); static protected $STATUS_VALUES = null; static public function getStatusFromIndex($index) { return self::$STATUS_INTEGERS[$index]; } static public function getStatusFromValue($value) { if (!self::$STATUS_VALUES) { self::STATUS_VALUES = array_flip(self::$STATUS_INTEGERS); } $values = strtolower($value); if (!isset(self::STATUS_VALUES[$value]) { throw new PropelException(sprintf('Status cannot take "%s" as a value', $value)); } return self::STATUS_VALUES[strtolower($value)]; } }
In Article.php:
class Article { public function setStatusName($value) { $this->setStatus(ArticlePeer::getStatusFromValue($value)); } public function getStatusName() { return ArticlePeer::getStatusFromIndex($this->getStatus()); } }
(Original tip from Fabien)
Comments on this snippet
In ArticlePeer.php: self::STATUS_VALUES shuld say self::$STATUS_VALUES self::STATUS_INTEGER shuld say self::$STATUS_INTEGER
and the variable values of the method getStatusFromValue is never used.
the if need and extra ) at the en of the line
In Article.php if you don't want to add a new field (StatusName) you could <pre> class Article { public function setStatus($value) { paret::setStatus(ArticlePeer::getStatusFromValue($value)); }
public function getStatus() { return ArticlePeer::getStatusFromIndex($this->getStatus()); } }
</pre>
i did as said in this article. Now i want to use it.
i am trying to use it in admin/backend. I am trying to add dropdown list for this field instead of input field. This generator.yml is not giving any error: generator: class: sfPropelAdminGenerator param: model_class: Category theme: default
Module is right.
This is a nice solution but the enum vales must be not be retrieved as fixed values (ie suppose i want to add or remove an enum value in future ), How could i do that without modifying the php code later .
@yousef: Use a 1:n table in the schema then. Like
document_typefor the base tabledocumentwithdocument.document_type_idI have one failed in line 18 of ArticlePeer.php
Warning: Illegal offset type in /srv/www/libros/lib/model/LibrosPeer.php on line 18
My generator.yml is =>
edit: fields: status: { name: Status, type: select_tag, params: peer_method=getStatusFromIndex related_class=Libros }
I need help is urgent i have proyect wednesday 4 - jun -2008
Thanks
By using the Admin Generator the simplest way is to use an _partical.
generator.yml
edit: display: MemberTyp: [ _typ ]
_typ.php
<?=select_tag('member[typ]', options_for_select(array('band' => 'Band', 'location' => 'Location'), $selected =$member->getTyp()));?>
The "selected"-option I use to select the current choice from database.
I hope it help! André Lademann
Consider that it may be just as good to use a varchar field instead of emulating an enum with an int. Depending on your data, the database performance may be similar, and the symfony code to implement it with a varchar field is easy and clean -- you can just add sfWidgetFormChoice() and sfValidatorChoice() in the object's form class, and that automatically gets used in the frontend and backend forms.
Fabien has a great example here, where he explains how to "convert the status widget to a choice":
http://www.symfony-project.org/blog/2008/10/14/new-in-symfony-1-2-make-your-choice
The Jobeet tutorial has a similar solution for job type, see "we want its value to be restricted to a list of choices: full time, part time, or freelance":
http://www.symfony-project.org/blog/2008/12/10/jobeet-day-10-the-forms
As for database performance, there are a number of articles on the web comparing (real) enum vs. varchar vs. int, etc. for this use. Here is one:
http://www.mysqlperformanceblog.com/2008/01/24/enum-fields-vs-varchar-vs-int-joined-table-what-is-faster/
Pete
in your schema.yml:
status: { type: varchar, sqltype: enum, size: "'open','close'" }
KISS! ;-)