Expression.php 1.53 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\db;
w  
Qiang Xue committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

/**
 * Expression represents a DB expression that does not need escaping or quoting.
 * When an Expression object is embedded within a SQL statement or fragment,
 * it will be replaced with the [[expression]] property value without any
 * DB escaping or quoting. For example,
 *
 * ~~~
 * $expression = new Expression('NOW()');
 * $sql = 'SELECT ' . $expression;  // SELECT NOW()
 * ~~~
 *
 * An expression can also be bound with parameters specified via [[params]].
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
26
class Expression extends \yii\base\Object
w  
Qiang Xue committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
{
	/**
	 * @var string the DB expression
	 */
	public $expression;
	/**
	 * @var array list of parameters that should be bound for this expression.
	 * The keys are placeholders appearing in [[expression]] and the values
	 * are the corresponding parameter values.
	 */
	public $params = array();

	/**
	 * Constructor.
	 * @param string $expression the DB expression
	 * @param array $params parameters
Qiang Xue committed
43
	 * @param array $config name-value pairs that will be used to initialize the object properties
w  
Qiang Xue committed
44
	 */
Qiang Xue committed
45
	public function __construct($expression, $params = array(), $config = array())
w  
Qiang Xue committed
46 47 48
	{
		$this->expression = $expression;
		$this->params = $params;
Qiang Xue committed
49
		parent::__construct($config);
w  
Qiang Xue committed
50 51 52 53 54 55 56 57 58 59
	}

	/**
	 * String magic method
	 * @return string the DB expression
	 */
	public function __toString()
	{
		return $this->expression;
	}
Zander Baldwin committed
60
}