开发者

Doctrine enum type not allows non enumerated values to be added?

I'm brand new to Doctrine and Code Igniter and I'm running into a problem with one of my tables.

I have a model that I thought would only allow 4 different letters (for testing at this point in time)

<?php
class Photo extends Doctrine_Record 
{
    public function setTableDefinition() 
    {
        $this->hasColumn('photo_path', 'string', 255, array('unique' => 'true'));       
        $this->hasColumn('category', 'enum', null,
            array('values' => array('a', 'b', 'c', 'd'))
        );
        $this->hasColumn('token', 'string', 255);
    }

    public function setUp() 
    {       
        $this->actAs('Timestampable')开发者_如何学Go;      
    }

    public function preInsert($event) 
    {
        $this->token = (sha1(rand(11111, 99999)));  
    }
}

I have a view that has a select box and an upload form

<?php echo validation_errors('<p class="error">','</p>'); ?>
    <div id="upload">
        <?php
        $categoryOptions= array(
        '' => '',
        'a' => 'a',
        'b' => 'b',
        'c' => 'c',
        '1' => '1'
        );
        echo form_open_multipart('admin/addImage');
        echo form_upload('userfile');
        echo form_dropdown('letter', $categoryOptions);
        echo form_submit('upload', 'Upload!');
        echo form_close();      
        ?>
    </div>

When I select '1' from the select box, I would expect Doctrine to throw an error and not insert that record, however it will insert it with a category of '1'. Is there some step I'm not doing so that the enum column restricts the input?

Thanks in advance.


And when I insert that field as '1' the letter 'a' shows up in the database (I guess because it's the first letter in the enum array?

Yes.

If i pass gibberish like 'ahjkh' the field appears to be null in the db even though I added 'notnull'=>true to the hastable line.

Are you sure you have it in the correct place? It should look something like the following, I think:

$this->hasColumn(
  'category', 
  'enum', 
  null,
  array(
    'values' => array('a', 'b', 'c', 'd'),
    'notnull' => true,
    'default' => 'a'
  )
);

So to use the validation hook, like prevalidate - is there way a way to get the enum array from the table definition, or do i need to create the array again in that function and just use in_array?

Column definitions are stored on the table so you can do something like this from within a model instance:

$enums = $this->getTable()->getEnumValues('yourEnumFieldName');

if(!in_array($value, $enums)){
   // push error to stack
}

Just for reference sake a fieldName is the name of the property on the model and the columnName is the actual name of the column. The two are not always the same so always make sure your passing the expected one when trying to get info from the Doctrine_Table.


This could have something to do with emulated versus native enum types, although I'm not sure because I always use native, and it works out. You might try setting native as well:

Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);

And if that doesn't do it, you can use one of the validation hooks to ensure it's in the enumeration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜