diff --git a/xpdo/om/mysql/xpdomanager.class.php b/xpdo/om/mysql/xpdomanager.class.php index 711202ec..87355d56 100644 --- a/xpdo/om/mysql/xpdomanager.class.php +++ b/xpdo/om/mysql/xpdomanager.class.php @@ -124,7 +124,10 @@ public function removeObjectContainer($className) { public function createObjectContainer($className) { $created= false; - if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) { + $connection = $this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true)); + if ($connection) { + $charset = $connection->getOption('charset'); + $collation = $connection->getOption('collation'); $instance= $this->xpdo->newObject($className); if ($instance) { $tableName= $this->xpdo->getTableName($className); @@ -144,7 +147,7 @@ public function createObjectContainer($className) { $fieldMeta = $this->xpdo->getFieldMeta($className, true); $columns = array(); foreach ($fieldMeta as $key => $meta) { - $columns[] = $this->getColumnDef($className, $key, $meta); + $columns[] = $this->getColumnDef($className, $key, $meta, array('charset' => $charset, 'collation' => $collation)); /* Legacy index support for pre-2.0.0-rc3 models */ if ($legacyIndexes && isset ($meta['index']) && $meta['index'] !== 'pk') { if ($meta['index'] === 'fulltext') { @@ -250,6 +253,12 @@ public function createObjectContainer($className) { $sql .= ")"; if (!empty($tableType)) { $sql .= " ENGINE={$tableType}"; + if (!empty($charset)) { + $sql .= " DEFAULT CHARSET={$charset}"; + if (!empty($collation)) { + $sql .= " COLLATE={$collation}"; + } + } } $created= $this->xpdo->exec($sql); if ($created === false && $this->xpdo->errorCode() !== '' && $this->xpdo->errorCode() !== PDO::ERR_NONE) { @@ -436,6 +445,13 @@ protected function getColumnDef($class, $name, $meta, array $options = array()) if (empty ($extra) && isset ($meta['extra'])) { $extra= ' ' . $meta['extra']; } + $charset = ''; + if (!empty($options['charset']) && in_array($this->xpdo->driver->getPhpType($dbtype), array('string'))) { + $charset = ' CHARACTER SET ' . $options['charset']; + if (!empty($options['collation'])) { + $charset .= ' COLLATE ' . $options['collation']; + } + } $default= ''; if (isset ($meta['default']) && !preg_match($lobsPattern, $dbtype)) { $defaultVal= $meta['default']; @@ -447,9 +463,9 @@ protected function getColumnDef($class, $name, $meta, array $options = array()) } $attributes= (isset ($meta['attributes'])) ? ' ' . $meta['attributes'] : ''; if (strpos(strtolower($attributes), 'unsigned') !== false) { - $result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $attributes . $null . $default . $extra; + $result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $attributes . $charset . $null . $default . $extra; } else { - $result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $null . $default . $attributes . $extra; + $result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $charset . $null . $default . $attributes . $extra; } return $result; }