YII mysql 迁移 oracle 小写处理

因为框架内联绝大多数代码是未进行引号包裹的。

oracle 纯sql的情况下,默认当做大写,影响很大。只能统一数据库和字段,表名大写转移。

Ar 兼容小写处理
编写 CActiveRecordO 集成 CActiveRecord

修改以下函数进行小写兼容

$model->field

public function __get($name) {
		$upname = strtoupper ( $name );
		if (isset ( $this->_attributes [$upname] )) {
			return $this->_attributes [$upname];
		} elseif (isset ( $this->getMetaData ()->columns [$upname] ))
			return null;
		elseif (isset ( $this->_related [$name] ))
			return $this->_related [$name];
		elseif (isset ( $this->getMetaData ()->relations [$name] ))
			return $this->getRelated ( $name );
		else
			return parent::__get ( $name );
	}

 

$model->field=val;

public function __set($name, $value) {
		$upname = strtoupper ( $name );
		if (($this->setAttribute ( $name, $value ) === false && $this->setAttribute ( $upname, $value ) === false)) {
			if (isset ( $this->getMetaData ()->relations [$name] ))
				$this->_related [$name] = $value;
			else
				parent::__set ( $name, $value );
		}
		$name = $upname;
	}

 

empty($model->field) //判断

public function __isset($name) {
		$upname = strtoupper ( $name );
		if (isset ( $this->_attributes [$upname] ))
			return true;
		elseif (isset ( $this->getMetaData ()->columns [$upname] ))
			return false;
		elseif (isset ( $this->_related [$name] ))
			return true;
		elseif (isset ( $this->getMetaData ()->relations [$name] ))
			return $this->getRelated ( $name ) !== null;
		else
			return parent::__isset ( $name );
	}

默认 getAttributes setAttributes 抱成别的函数进行重写,因为内部调用太多地方,不可直接重写该函数,另外编写函数getAttributesWithO 之类的即可

	public function getAttributesWithO($names = true) {
		$attributes = $this->getAttributes ( $names );
		$attributes = array_change_key_case ( $attributes, CASE_LOWER );
		return $attributes;
	}
	public function setAttributesWithO($attributes = array(), $safeOnly = true, $hasClob = false) {
		$attributes = array_change_key_case ( $attributes, CASE_UPPER );
		if ($hasClob && $this->getMetaData ()->columns && is_array ( $this->getMetaData ()->columns )) {
			foreach ( $this->getMetaData ()->columns as $key => $column ) {
				if (stristr ( $column->dbType, "CLOB" ) && empty ( $attributes [$key] )) {
					$attributes [$key] = '';
				}
			}
		}
		$this->setAttributes ( $attributes, $safeOnly );
	}
	public function getClobByKey($key) {
		if (! empty ( $this->$key ) && is_object ( $this->$key ) && get_class ( $this->$key ) == 'OCI-Lob') {
			$this->$key = $this->$key->read ( 50000 );
		}
	}

pdo 对clob各种奇葩问题。需要调整pdo为oci8相关处理

http://www.yiiframework.com/extension/oci8pdo/

	public function afterFind() {
		$this->getClobByKey ( 'description' );
		return true;
	}

内部组件需要进行特殊处理。

主要出现在需要调用数据库实现相关功能的组件,如:CDbAuthManager,CDbHttpSession

重写相关即可

主要为大小写冲突问题。

public function hasItemChild($itemName,$childName)
	{
		return $this->db->createCommand()
			->select('parent')
			->from($this->itemChildTable)
			->where('parent=:parent AND child=:child', array(
				':parent'=>$itemName,
				':child'=>$childName))
			->queryScalar() !== false;
	}

select (‘parent’) 会返回 select “parent” 导致大小写异常,where parent =又是小写处理,纠结吧。。。

所以只能将被小写了的地方进行大写转换处理。

将被大写返回的数组,需要进行小写转换下。

		$row=array_change_key_case ( $row, CASE_LOWER );

oci 修复个setFetchModel的小异常问题,兼容有点问题,不过只在权限判断中出现,其它暂时未发现

http://www.php.net/manual/en/pdostatement.setfetchmode.php

public function setFetchMode($mode, $colClassOrObj = null, array $ctorArgs = array()) {
		// yii 内部调用
// 		parent::setFetchMode($mode,$colClassOrObj,$ctorArgs);
// 		return true;
		if ($mode == PDO::FETCH_COLUMN||$mode==PDO::FETCH_ASSOC) {
			$this->_fetchMode = $mode;
			return true;
		}
		// 52: $this->_statement->setFetchMode(PDO::FETCH_ASSOC);
		if ($colClassOrObj !== null || ! empty ( $ctorArgs )) {
			throw new PDOException ( 'Second and third parameters are not implemented for Oci8PDO_Statement::setFetchMode()' );
			// see http://www.php.net/manual/en/pdostatement.setfetchmode.php
		}
		$this->_fetchMode = $mode;
		return true;
	}

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注