forked from tom--/yii2-dynamic-ar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueExpression.php
70 lines (66 loc) · 2.06 KB
/
ValueExpression.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace spinitron\dynamicAr;
/**
* A ValueExpression object represents the value of a dynamic attribute that DynamicActiveRecord
* uses directly (unescaped) in SQL instead of using
* [PDOStatement::bindValue](http://php.net/manual/en/pdostatement.bindvalue.php).
*
* Example:
*
* ```php
* $model->width = new ValueExpression(123.456);
* ```
*
* You may optionally give an
* [SQL datatype](https://mariadb.com/kb/en/mariadb/dynamic-columns/#datatypes)
* as second argument of the constructor, for example:
*
* ```php
* $model->joined = new ValueExpression('"2015-06-01 12:30:00"', 'DATETIME');
* $model->price = new ValueExpression(4.99, 'DECIMAL(6,2)');
* ```
*
* > NOTE: because values are not escaped or bound using PDO, for string values (including date
* and time types) you must provide the SQL quotes around the string in the value.
*
* > NOTE: using ValueExpression for the value of a column attribute rather than a dynamic attribute
* will cause an error.
*
* @package spinitron\dynamicAr
*/
class ValueExpression extends \yii\base\Object
{
/**
* @var mixed The dynamic attribute value.
*/
public $value;
/**
* @var null|string The value's
* [SQL datatype](https://mariadb.com/kb/en/mariadb/dynamic-columns/#datatypes) (optional)
*/
public $type;
/**
* Constructor.
*
* @param mixed $value The dynamic attribute value.
* @param string $type The value's
* [SQL datatype](https://mariadb.com/kb/en/mariadb/dynamic-columns/#datatypes). Omit or
* set null to not specify a datatype.
* @param array $config Name-value pairs to initialize object properties.
*/
public function __construct($value, $type = null, $config = [])
{
$this->value = $value;
$this->type = $type;
parent::__construct($config);
}
/**
* To string magic method.
*
* @return string The dynamic value's SQL expression.
*/
public function __toString()
{
return $this->value . ($this->type !== null ? ' as ' . $this->type : '');
}
}