2013-04-12 42 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