简体   繁体   中英

Using Mysql data type Set with Doctrine 1.2

We're using a database from a legacy system which includes fields with type Set ( http://dev.mysql.com/doc/refman/5.0/en/set.html )

We need to generate base models constantly from the database. The fields that are type Set in the database are generated as type Text in the base models.

Manually changing the fields to 'set' gives us the desired result, but is there any way to get doctrine to generate the models correctly to begin with?

I managed to resolve the issue. You need to override the DataDict class used for the connection adapter (in our case, Doctrine_DataDict_Mysql )

I registered a custom connection class with

$manager = Doctrine_Manager::getInstance();
$manager->registerConnectionDriver('mysql', 'My_Doctrine_Connection_Mysql');

In My_Doctrine_Connection_Mysql :

class My_Doctrine_Connection_Mysql extends Doctrine_Connection_Mysql
{
    /**
     * Data Dict(ator|ionary)?
     * @var My_Doctrine_DataDict_Mysql
     */
    protected $_dataDict;

    /**
     * __get Overloading method
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        if ('dataDict' === $name) {
            if (null === $this->_dataDict) {
                $this->_dataDict = new My_Doctrine_DataDict_Mysql($this);
            }

            return $this->_dataDict;
        }

        return parent::__get($name);
    }
}

And in My_Doctrine_DataDict_Mysql :

class My_Doctrine_DataDict_Mysql extends Doctrine_DataDict_Mysql
{
    /**
     * @see parent::getPortableDecleration
     * @param array $field
     * @return array
     */
    public function getPortableDeclaration(array $field)
    {
        $definition = parent::getPortableDeclaration($field);

        // Normalising field type. Pulled almost exactly from parent
        $length = null;
        $dbType = strtolower($field['type']);
        $dbType = strtok($dbType, '(), ');
        if ($dbType == 'national') {
            $dbType = strtok('(), ');
        }
        if (isset($field['length'])) {
            $length = $field['length'];
        } else {
            $length = strtok('(), ');
            $decimal = strtok('(), ');
        }

        // Set definition
        switch ($dbType) {
            case 'set':
                $definition['type'] = array('set');
                break;
        }

        return $definition;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM