Skip to content

Loading…

[Enh] Gii could filter ENUM fields (rules and forms) #292

Closed
qiangxue opened this Issue · 8 comments

7 participants

@qiangxue
Yii Software LLC member

As for now, Yii treats ENUM columns as normal string fields: it finds the widest possible value and uses it as max length. But it would be easy for Gii to add a rule that checks if the value entered is among the authorized ones of the ENUM.

Here is an ugly hack as a proof of concept (there's no visible doc on the allow syntax of this description, I hope it will appear below):

foreach ($columns as $name => $column) {
    if (strncmp($column->dbType, 'enum', 4) === 0 && preg_match('/\((.*)\)/', $column->dbType, $matches)) {
        $values = str_replace("''", "\'", $matches[1]); // won't work if there are "\" characters
        echo "\t\t\tarray('{$name}', 'in', array({$values})), // enum\n";
    }
}

It won't work with some pathological values of the enum, but it doesn't matter. The developper can fix thoses rare cases himself. The model would still be DB-vendor independant, as this only affects Gii.

Though this works, I believe it could be better if CDbColumnSchema had a "$enum = null" attribute. It would then allow CrudCode::generateActiveField() to use it and build a HTML select for the enum fields. This would enhance the view "_form.php" produced by the default Gii CRUD templates. CGridView could also use this for its default filters.

There have been several posts about this in the forums, but AFAIK with no word from the staff. If you feel this enhancement is useful but have more urgent tasks, I could submit a patch (or a git fork).

Migrated from http://code.google.com/p/yii/issues/detail?id=2945


earlier comments

qiang.xue said, at 2012-01-01T03:37:10.000Z:

set for 1.1.10 milestone

qiang.xue said, at 2012-01-01T03:37:36.000Z:

set for 1.1.10 milestone

@samdark
Yii Software LLC member

This one can be useful.

@cebe
Yii Software LLC member

@cebe will try to work on this when I find some time...

@leokolln

So.. no news on this?

@cebe
Yii Software LLC member

nope, sorry. Possibility for me finding time is near to zero.

@SL1210

Would love this functionality please!

@enigmatix

@cebe cebe I would deploy this an enum field as a dropdown through Gii. I've written some funcitonal code for it, and would love to contribute actively to this - especially if you don't have time yourself.

@samdark
Yii Software LLC member

Would be great. Please prepare a pull request and we'll check it.

@enigmatix enigmatix added a commit to enigmatix/yii2 that referenced this issue
@enigmatix enigmatix Added ability to generate dropdown from Enum field
Alternatively, if statement could be: if(substr($column->dbType, 0, 4) === 'enum'), however expected implemented option to be more robust across other database formats.

Created after followup on this issue:  yiisoft/yii#292 (comment)
e636174
@qiangxue qiangxue pushed a commit to yiisoft/yii2-gii that referenced this issue
@enigmatix enigmatix Added ability to generate dropdown from Enum field
Alternatively, if statement could be: if(substr($column->dbType, 0, 4) === 'enum'), however expected implemented option to be more robust across other database formats.

Created after followup on this issue:  yiisoft/yii#292 (comment)
c06f1ce
@evan-king

I recently wanted to use an enum but felt the lack of correct validation in our auto-generated models (no tweaking allowed) in conjunction with MySQL's silent acceptance of invalid inputs lent too great a vector for coding errors.

Though I'd like to contribute code against this, our resources are currently too constrained for it - perhaps I can set aside some personal time for it but that's a long shot. As a result, we have ended up using a lookup table just for a single "context-explicit values" case of the dreaded 3-value boolean storage.

Mark me among those wishing for more robust and restrictive enum handling.

@samdark samdark closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.