Search code examples
phpmysqlpropel

propel always select the default value of enum-columns?


I'm quite irritated at unterstanding Propel's handling of enum-columns (using Propel 1.6.9 and MySQL). It seems that it always returns the default values.

The CREATE-Statement of the table:

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 ;

This is the relevant part of my 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>



I've got 2 enum-columns, amountType and priceType. I select 2 rows from this table

  1. one with amountType == entity | priceType == per night
  2. one with amountType == person| priceType == per day

The default value of amountType is entity, of priceType per night.

I fetch the rows this way:

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

    return $variants;
}

and the return is:

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

after using the getters:

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

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

simply wrong.

I read about the fact that Propel interprets the type="enum" another way than MySQL does, and that it would be nessecary to set the sqlType in the schema.xml. I did this as mentioned above, rebuilt, but no change.

  1. Isn't it possible to fetch the stated enum-value?
  2. Or is my schema.xml incorrect?
  3. Or do I fetch the wrong way?

Solution

  • ENUM Columns

    Although stored in the database as integers, ENUM columns let users manipulate a set of predefined values, without worrying about their storage. http://propelorm.org

    If you set your column in propel as ENUM, declare in sql as INTEGER.

    Or, preferably, you can try type="VARCHAR" sqlType="ENUM('...')".