propel始终选择枚举列的默认值?

时间:2013-04-12 16:09:18

标签: php mysql propel

我非常恼火地看到Propel对枚举列的处理(使用Propel 1.6.9和MySQL)。它似乎总是返回默认值。

表格的CREATE-Statement:

CREATE TABLE IF NOT EXISTS `offerVariant` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `offerID` int(11) unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  [1] -> `amountType` enum('entity','person') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'entity',
  `unitCount` smallint(5) unsigned DEFAULT NULL,
  [2] -> `priceType` enum('per night','per day','per hour','flat rate') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'per night',
  `tax` tinyint(3) NOT NULL DEFAULT '7',
  `maxPrice` decimal(12,2) NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`id`),
  KEY `offerID` (`offerID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

这是我的schema.xml的相关部分:

<table name="offerVariant">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
        <column name="offerID" type="integer" size="11" required="true" />
        <column name="name" type="varchar" size="255" required="true" />
        <column name="description" type="longvarchar" required="true" />
        [1] -> <column name="amountType" type="enum" valueSet="entity, person" sqlType="ENUM('entity','person')" required="true" />
        <column name="unitCount" type="smallint" size="5" required="false" />
        [2] -> <column name="priceType" type="enum" valueSet="per night, per day, per hour, flat rate" sqlType="ENUM('per night','per day','per hour','flat rate')" required="true" />
        <column name="tax" type="tinyint" size="3" required="true" />
        <column name="maxPrice" type="decimal" size="14" required="true" />
        <foreign-key foreignTable="offer" refPhpName="offerVariant">
            <reference local="offerID" foreign="id"/>
        </foreign-key>
    </table>



我有2个枚举列, amountType priceType 。 我从这张表中选择了2行

  1. 一个 amountType == entity | priceType == 每晚
  2. 一个 amountType == person | priceType == 每天
  3. amountType 的默认值为实体 priceType 每晚

    我以这种方式获取行:

    public function selectVariantsByOffer($offerid){
        $variants = OffervariantQuery::create()
        ->filterByOfferId($offerid)
        ->find();
    
        return $variants;
    }
    

    并且返回:

    [0] => Offervariant Object
      (
        [startCopy:protected] => 
        [id:protected] => 1
        [amounttype:protected] => 0
        [pricetype:protected] => 0
        [...]
      )
    
    [1] => Offervariant Object
      (
        [startCopy:protected] => 
        [id:protected] => 2
        [amounttype:protected] => 0
        [pricetype:protected] => 0
        [...]
      )
    
    使用getter后

    [0] => Array
        (
            [id] => 1
            [...]
            [amountType] => entity
            [priceType] => per night
        )
    
    [1] => Array
        (
            [id] => 2
            [...]
            [amountType] => entity
            [priceType] => per night
        )
    

    完全错了。

    我读到了这样一个事实:Propel解释了type =“enum”,而不是像MySQL那样,并且在schema.xml中设置sqlType是不必要的。我如上所述做了这个,重建了,但没有改变。

    1. 是否有可能获取所述的枚举值?
    2. 或者我的schema.xml不正确吗?
    3. 或者我取错了方法?

1 个答案:

答案 0 :(得分:5)

  

ENUM列

     

虽然以整数存储在数据库中,但ENUM列允许用户操作一组预定义值,而不必担心其存储。 http://propelorm.org

如果您将列中的列设置为ENUM,请在sql中声明为INTEGER

或者,您最好尝试type="VARCHAR" sqlType="ENUM('...')"

相关问题