yii2-gii icon indicating copy to clipboard operation
yii2-gii copied to clipboard

Implemented enum

Open uldisn opened this issue 5 years ago • 8 comments

Q A
Is bugfix? no
New feature? yes
Breaks BC? no
Tests pass? yes

Generate:

  • enum value constants
  • rules type in
  • opts
  • get enum field label (mybe rename )
  • is[EnumField][enumValue]
class Test extends \yii\db\ActiveRecord 
{
    const TYPE_PARTNER = 'PARTNER';
    const TYPE_PUBLIC = 'PUBLIC';
    const TYPE_INTERNAL = 'INTERNAL';
    const TYPE_OWNER = 'OWNER';

    public function rules()
    {
        return [
            [['type'], 'string'],
            [['position'], 'string', 'max' => 50],
            ['type', 'in', 'range' => [
                self::TYPE_PARTNER,
                self::TYPE_PUBLIC,
                self::TYPE_INTERNAL,
                self::TYPE_OWNER,
            ]
            ],
        ];
    }

   /**
     * column type ENUM value labels
     * @return array
     */
    public static function optsType()
    {
        return [
            self::TYPE_PARTNER => 'PARTNER',
            self::TYPE_PUBLIC => 'PUBLIC',
            self::TYPE_INTERNAL => 'INTERNAL',
            self::TYPE_OWNER => 'OWNER',
        ];
    }

    /**
    * @return string
    */
    public function displayType()
    {
        return self::optsType()[$this->type];
    }

    /**
     * @return bool
     */
    public function isTypePARTNER()
    {
        return $this->type === self::TYPE_PARTNER;
    }

    /**
     * @return bool
     */
    public function isTypePUBLIC()
    {
        return $this->type === self::TYPE_PUBLIC;
    }

    /**
     * @return bool
     */
    public function isTypeINTERNAL()
    {
        return $this->type === self::TYPE_INTERNAL;
    }

    /**
     * @return bool
     */
    public function isTypeOWNER()
    {
        return $this->type === self::TYPE_OWNER;
    }
}

uldisn avatar May 24 '20 20:05 uldisn

Thank you for putting effort in the improvement of the Yii framework. We have reviewed your pull request.

In order for the framework and your solution to remain stable in the future, we have a unit test requirement in place. Therefore we can only accept your pull request if it is covered by unit tests.

Could you add these please?

Thanks!

P.S. If you have any questions about the creation of unit tests? Don't hesitate to ask for support. More information about unit tests

This is an automated comment, triggered by adding the label pr:request for unit tests.

yii-bot avatar May 25 '20 09:05 yii-bot

Unit test done

uldisn avatar Dec 26 '20 16:12 uldisn

I've got few concerns:

  1. How this will work with MSSQL? IIRC ENUM dbType is recognized but enumValues are left empty.
  2. Overall this needs some tests with real DB engines.
  3. Are we cool with that much code being generated here?
  4. What do you think about having these optional/configured?

bizley avatar Dec 28 '20 10:12 bizley

I've got few concerns:

  1. How this will work with MSSQL? IIRC ENUM dbType is recognized but enumValues are left empty. This is a problem for MS SQL users
  2. Overall this needs some tests with real DB engines. On MySql and MariaDB enum use more two years
  3. Are we cool with that much code being generated here? If use enum, is very useful. Code generate, if model has enums. Setting like $model->setTypePublic() also would also be good
  4. What do you think about having these optional/configured? Actually gii model form is so big. No need.

In giiant enum use long time: https://github.com/schmunk42/yii2-giiant/blob/master/src/generators/model/Generator.php#L295

uldisn avatar Dec 28 '20 10:12 uldisn

  1. How this will work with MSSQL? IIRC ENUM dbType is recognized but enumValues are left empty.

Please provide me MSSQL schema dump like https://github.com/yiisoft/yii2-gii/pull/434/files#diff-eef4be0410fa93517728588e0c838dd2f9afb86ff7148c7f7d7636de99df3b00R486

uldisn avatar Dec 29 '20 08:12 uldisn

@samdark! Actual PR still waiting. @bizley added additional strange requirements. It work.

uldisn avatar Mar 30 '21 16:03 uldisn

@bizley

How this will work with MSSQL? IIRC ENUM dbType is recognized but enumValues are left empty.

mssql don't have enum/set types and they not required in sql standards

WinterSilence avatar Mar 31 '21 01:03 WinterSilence

What do you mean by "strange"? These are legitimate concerns. You are just adding the feature but we will have to maintain it later on - I would like to at least be sure it works in most of the cases. And I'm not at the moment. I would like to see it tested with PostgreSQL since there is schema already in the tests added. And still no one said anything about the amount of additional code generated here (that is apart from you two guys, it's nice that you support each other like that).

bizley avatar Mar 31 '21 09:03 bizley

I woke up

uldisn avatar Oct 31 '23 19:10 uldisn

Apparently he went back to sleep.

@samdark, what do you think about removing this PR from the 2.2.7 milestone and releasing the version?

thiagotalma avatar Nov 28 '23 12:11 thiagotalma

Apparently he went back to sleep.

I define this initiative as illegal

uldisn avatar Nov 28 '23 15:11 uldisn

I define this initiative as illegal

I prefer to believe that this was some kind of slang, as I don't understand English well.

But if you were bothered by my joke, I apologize.

My intention was just to clear the milestone so that a new version can be released.

thiagotalma avatar Nov 28 '23 17:11 thiagotalma

I define this initiative as illegal

I prefer to believe that this was some kind of slang, as I don't understand English well.

But if you were bothered by my joke, I apologize.

My intention was just to clear the milestone so that a new version can be released.

Don't take it seriously. When should I wake up?

uldisn avatar Nov 28 '23 17:11 uldisn

@uldisn time to wake up. Please add a line for CHANGELOG. Then I'll merge it.

samdark avatar Jan 29 '24 18:01 samdark

@uldisn time to wake up. Please add a line for CHANGELOG. Then I'll merge it.

Done! Good night!

uldisn avatar Jan 29 '24 18:01 uldisn

Thank you!

samdark avatar Feb 01 '24 09:02 samdark