Yii Framework Forum: application wide functions - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

application wide functions Rate Topic: -----

#1 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 25 May 2012 - 12:01 PM

note:
an example function CDbExpression() to handle MS-SQL getdate() vs MySQL NOW() ;
you can have whatever functions here which would be used application wide.

add files in \protected\components\functions\Fn.php

class fn extends CApplicationComponent
{
	public function init() {
	}
	
	/**
	 * handle NOW() function for ms-sql server
	 * @param string $exp
	 * @return string
	 */
	public static function CDbExpression($exp, $db='db') {
		if($exp == 'NOW()') {
			if (substr(Yii::app()->{$db}->connectionString, 0, 7) === 'sqlsrv:') {
				return new CDbExpression('GETDATE()');
			}
		}

		//otherwise
		return new CDbExpression($exp);
	}
	
}



in protected\config\main.php add line

'import'=>array(
//~~
	'application.components.functions.*',
//~~


in any code, replace
new CDbExpression('NOW()');


with

fn::CDbExpression('NOW()');

I enjoy the Yii.sy coding life here.
1

#2 User is offline   raickonen 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-July 12
  • Location:Porto Alegre, RS, Brazil

Posted 28 May 2013 - 08:35 PM

Thanks man, but it didn't work. I always receive:

GETDATE() as return.

Any Ideas?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users