David Weinraub
David Weinraub

Reputation: 14184

Doctrine 1.2 models returning integer fields as string values using Zend Framework and MySQL 5

Any idea why my Doctrine 1.2 models are returning string values for integer-defined fields?

Base model defined as:

abstract class Kwis_Model_Base_User extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('user');
        $this->hasColumn('id', 'integer', 4, array(
             'unsigned' => true,
             'primary' => true,
             'autoincrement' => true,
             ));

        // more fields
        // ...
    }

Table Model is defined as:

class Kwis_Model_UserTable extends Kwis_Model_BaseTable
{

    /**
     * Returns an instance of this class.
     *
     * @return object Kwis_Model_UserTable
     */
    public static function getInstance()
    {
        $table = Doctrine_Core::getTable('Kwis_Model_User');
        return $table;
    }

    // other methods
    // ...
}

Sample controller code:

class TestController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $userTable = Kwis_Model_UserTable::getInstance();
        $user = $userTable->find(1);
        echo "<pre>" . var_dump($user->id) . "</pre>"; die();
    }
}

Produced output:

 string '1' (length=1)

Any ideas, greatly appreciated. ;-)

Upvotes: 1

Views: 1361

Answers (2)

Zaqwsx
Zaqwsx

Reputation: 7793

Also as an answer to below, bitwise operations on strings left- and right-hand will operate on the ASCII value.

You can cast int as int using a hydration listener. Either create a new hydration listener and add it to the record(1), or just use the existing hook templates in the record object(2).

1: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/event-listeners/pl#record-listeners

2:

   abstract class My_Rec extends Doctrine_Record {
    function postHydrate($event) {
        $data = $event->data;
        $data['status'] = (int) $data['status'];
        $event->data = $data;       
    }
}

Upvotes: 3

Konr Ness
Konr Ness

Reputation: 2153

Isn't that the benefit of using typeless variables in PHP? What problem is this causing? You can still do mathematic operations on it if you want. Although, I would suspect that you'd never need to do any actions on a user ID other than string comparison.

Upvotes: 0

Related Questions