2013-04-12 40 views
1

我对unpeltanding的propel处理enum-columns(使用Propel 1.6.9和MySQL)非常恼火。它似乎总是返回默认值。推动总是选择枚举列的默认值?

的CREATE-声明表:

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 and priceType。 我从这个表

  1. 一个选择2行与amountType == 实体 | priceType == 每晚
  2. 一个与amountType == | priceType == 每天

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

使用干将后:

[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. 还是我错误的方式?

回答

5

ENUM列

虽然存储在数据库中整数,ENUM列让用户操控一组预定义的值,而不用担心他们的存储。 http://propelorm.org

如果在推进设置您的列ENUM,在SQL声明为INTEGER

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

+0

我做了它的varchar方式,它的作品就像一个魅力。谢谢! – 32bitfloat

+0

是的,这是绝对有效的..我也使用该varchar解决方案 –

+0

只是做了相同的w /'text'字段,'type =“VARCHAR”sqlType =“TEXT”',因为我不想对待文本字段就像PHP中的数组一样。 – quickshiftin