Expression.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. /**
  9. * Expression represents a DB expression that does not need escaping or quoting.
  10. *
  11. * When an Expression object is embedded within a SQL statement or fragment,
  12. * it will be replaced with the [[expression]] property value without any
  13. * DB escaping or quoting. For example,
  14. *
  15. * ```php
  16. * $expression = new Expression('NOW()');
  17. * $now = (new \yii\db\Query)->select($expression)->scalar(); // SELECT NOW();
  18. * echo $now; // prints the current date
  19. * ```
  20. *
  21. * Expression objects are mainly created for passing raw SQL expressions to methods of
  22. * [[Query]], [[ActiveQuery]], and related classes.
  23. *
  24. * An expression can also be bound with parameters specified via [[params]].
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @since 2.0
  28. */
  29. class Expression extends \yii\base\BaseObject implements ExpressionInterface
  30. {
  31. /**
  32. * @var string the DB expression
  33. */
  34. public $expression;
  35. /**
  36. * @var array list of parameters that should be bound for this expression.
  37. * The keys are placeholders appearing in [[expression]] and the values
  38. * are the corresponding parameter values.
  39. */
  40. public $params = [];
  41. /**
  42. * Constructor.
  43. * @param string $expression the DB expression
  44. * @param array $params parameters
  45. * @param array $config name-value pairs that will be used to initialize the object properties
  46. */
  47. public function __construct($expression, $params = [], $config = [])
  48. {
  49. $this->expression = $expression;
  50. $this->params = $params;
  51. parent::__construct($config);
  52. }
  53. /**
  54. * String magic method.
  55. * @return string the DB expression.
  56. */
  57. public function __toString()
  58. {
  59. return $this->expression;
  60. }
  61. }