mysql custom global defined variable

前端 未结 4 1627
逝去的感伤
逝去的感伤 2021-01-21 00:10

In my database design, I tend to store some variable that is meant to be acting as a ROLE or TYPE as SMALLINT. For example :



        
4条回答
  •  青春惊慌失措
    2021-01-21 01:15

    Your approach is fine, if you want to see the values in MySQL instead of 1, 2, 3 etc. then consider this:

    define('HOUSE_SMALL_TYPE', 'HOUSE_SMALL_TYPE');
    define('HOUSE_MEDIUM_TYPE', 'HOUSE_MEDIUM_TYPE');
    

    Then in MySQL you can use:

    SELECT * FROM house  WHERE type = 'HOUSE_SMALL_TYPE';
    

    You just need to remember that you cannot just jam any value you like into house.type without having support for it in PHP.

    Even better consider this:

    class HouseType {
      const SMALL = 'SMALL';
      const MEDIUM = 'MEDIUM';
    }
    

    or

    class House {
      const TYPE_SMALL = 'SMALL';
      const TYPE_MEDIUM = 'MEDIUM';
    }
    

    because then you can use HouseType::SMALL or House::TYPE_SMALL in your PHP code rather than using a global define. By doing this you may benefit from code completion in some IDE's.

提交回复
热议问题