Skip to content

Instantly share code, notes, and snippets.

@havvg
Created December 8, 2011 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save havvg/1446917 to your computer and use it in GitHub Desktop.
Save havvg/1446917 to your computer and use it in GitHub Desktop.
true MySQL ENUM with Propel
<?php
class MyModel extends BaseMyModel
{
public function hydrate($row, $startcol = 0, $rehydrate = false)
{
try {
$this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
$this->type = ($row[$startcol + 1] !== null) ? $row[$startcol + 1] : null;
$this->resetModified();
$this->setNew(false);
if ($rehydrate) {
$this->ensureConsistency();
}
return $startcol + 2;
} catch (Exception $e) {
throw new PropelException("Error populating MyModel object", $e);
}
}
}
<?php
class MyModelPeer extends BaseMyModelPeer
{
/** The enumerated values for this table */
protected static $enumValueSets = array(
self::TYPE => array(
MyModelPeer::TYPE_IN => MyModelPeer::TYPE_IN,
MyModelPeer::TYPE_OUT => MyModelPeer::TYPE_OUT,
),
);
}
<?php
class MyModelTest extends \PHPUnit_Framework_TestCase
{
public function testEnum()
{
$obj = new MyModel();
$obj->setType(MyModelPeer::TYPE_IN);
$this->assertTrue((bool) $obj->save());
$expected = array(
'IN' => 'IN',
'OUT' => 'OUT',
);
$this->assertEquals($expected, MyModelPeer::getValueSet(MyModelPeer::TYPE));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- ... -->
<column name="type" type="enum" valueSet="IN,OUT" sqlType="ENUM('IN','OUT')" required="true" />
<!-- ... -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment