2

我对不理解 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 个枚举列,amountTypepriceType。我从此表中选择 2 行

  1. 一个有amountType == entity | priceType ==每晚
  2. 一个有amountType == person | priceType ==每天

amountType的默认值为entitypriceType per night

我以这种方式获取行:

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
    [...]
  )

使用吸气剂后:

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

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

完全错了。

我了解到 Propel 以不同于 MySQL 的方式解释 type="enum" 的事实,并且在 schema.xml 中设置 sqlType 是必要的。我按照上面提到的那样做了,重建了,但没有改变。

  1. 不能获取规定的枚举值吗?
  2. 还是我的 schema.xml 不正确?
  3. 还是我取错了方法?
4

1 回答 1

6

枚举列

尽管以整数形式存储在数据库中,但 ENUM 列允许用户操作一组预定义的值,而无需担心它们的存储。http://propelorm.org

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

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

于 2013-04-12T16:26:55.297 回答