类 yii\db\Expression
| 继承 | yii\db\Expression » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable, yii\db\ExpressionInterface |
| 可用版本 | 2.0 |
| 源代码 | https://github.com/yiisoft/yii2/blob/master/framework/db/Expression.php |
Expression 表示不需要转义或引用的数据库表达式。
当一个 Expression 对象嵌入到 SQL 语句或片段中时,它将被替换为 $expression 属性值,不会进行任何数据库转义或引用。例如:
$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar(); // SELECT NOW();
echo $now; // prints the current date
Expression 对象主要用于将原始 SQL 表达式传递给 yii\db\Query、yii\db\ActiveQuery 及其相关类的方法。
表达式也可以绑定通过 $params 指定的参数。
公共属性
| 属性 | 类型 | 描述 | 定义于 |
|---|---|---|---|
| $expression | string | 数据库表达式 | yii\db\Expression |
| $params | array | 应该为该表达式绑定的参数列表。键是出现在 $expression 中的占位符,值是相应的参数值。 | yii\db\Expression |
公共方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| __call() | 调用不是类方法的命名方法。 | yii\base\BaseObject |
| __construct() | 构造函数。 | yii\db\Expression |
| __get() | 返回对象属性的值。 | yii\base\BaseObject |
| __isset() | 检查属性是否已设置,即定义且不为空。 | yii\base\BaseObject |
| __set() | 设置对象属性的值。 | yii\base\BaseObject |
| __toString() | 字符串魔术方法。 | yii\db\Expression |
| __unset() | 将对象属性设置为 null。 | yii\base\BaseObject |
| canGetProperty() | 返回一个值,指示是否可以读取属性。 | yii\base\BaseObject |
| canSetProperty() | 返回一个值,指示是否可以设置属性。 | yii\base\BaseObject |
| className() | 返回此类的完全限定名称。 | yii\base\BaseObject |
| hasMethod() | 返回一个值,指示是否定义了方法。 | yii\base\BaseObject |
| hasProperty() | 返回一个值,指示是否定义了属性。 | yii\base\BaseObject |
| init() | 初始化对象。 | yii\base\BaseObject |
属性详细信息
方法详细信息
| public mixed __call ( $name, $params ) | ||
| $name | string |
方法名称 |
| $params | array |
方法参数 |
| 返回值 | mixed |
方法返回值 |
|---|---|---|
| 抛出 | yii\base\UnknownMethodException |
当调用未知方法时 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
构造函数。
| public void __construct ( $expression, $params = [], $config = [] ) | ||
| $expression | string |
数据库表达式 |
| $params | array |
参数 |
| $config | array |
将用于初始化对象属性的名称-值对 |
public function __construct($expression, $params = [], $config = [])
{
$this->expression = $expression;
$this->params = $params;
parent::__construct($config);
}
定义于: yii\base\BaseObject::__get()
返回对象属性的值。
不要直接调用此方法,因为它是 PHP 魔术方法,当执行 $value = $object->property; 时将隐式调用。
另见 __set()。
| public mixed __get ( $name ) | ||
| $name | string |
属性名称 |
| 返回值 | mixed |
属性值 |
|---|---|---|
| 抛出 | yii\base\UnknownPropertyException |
如果属性未定义 |
| 抛出 | yii\base\InvalidCallException |
如果属性是只写属性 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定义于: yii\base\BaseObject::__isset()
检查属性是否已设置,即定义且不为空。
不要直接调用此方法,因为它是一个 PHP 魔术方法,会在执行 isset($object->property) 时被隐式调用。
注意,如果属性未定义,将返回 false。
| public boolean __isset ( $name ) | ||
| $name | string |
属性名或事件名 |
| 返回值 | boolean |
命名的属性是否已设置(非空)。 |
|---|---|---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定义于: yii\base\BaseObject::__set()
设置对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,会在执行 $object->property = $value; 时被隐式调用。
另请参见 __get().
| public void __set ( $name, $value ) | ||
| $name | string |
属性名或事件名 |
| $value | mixed |
属性值 |
| 抛出 | yii\base\UnknownPropertyException |
如果属性未定义 |
|---|---|---|
| 抛出 | yii\base\InvalidCallException |
如果属性是只读的 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
字符串魔术方法。
| public string __toString ( ) | ||
| 返回值 | string |
数据库表达式。 |
|---|---|---|
public function __toString()
{
return $this->expression;
}
定义于: yii\base\BaseObject::__unset()
将对象属性设置为 null。
不要直接调用此方法,因为它是一个 PHP 魔术方法,会在执行 unset($object->property) 时被隐式调用。
注意,如果属性未定义,此方法将不会执行任何操作。如果属性是只读的,它将抛出异常。
| public void __unset ( $name ) | ||
| $name | string |
属性名称 |
| 抛出 | yii\base\InvalidCallException |
如果属性是只读的。 |
|---|---|---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
定义于: yii\base\BaseObject::canGetProperty()
返回一个值,指示是否可以读取属性。
一个属性是可读的,如果
- 该类有一个与指定名称关联的 getter 方法(在这种情况下,属性名不区分大小写);
- 该类有一个与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参见 canSetProperty().
| public boolean canGetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
该属性是否可读 |
|---|---|---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定义于: yii\base\BaseObject::canSetProperty()
返回一个值,指示是否可以设置属性。
一个属性是可写的,如果
- 该类有一个与指定名称关联的 setter 方法(在这种情况下,属性名不区分大小写);
- 该类有一个与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参见 canGetProperty().
| public boolean canSetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
该属性是否可写 |
|---|---|---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class 代替。
定义于: yii\base\BaseObject::className()
返回此类的完全限定名称。
| public static string className ( ) | ||
| 返回值 | string |
此类的完全限定名。 |
|---|---|---|
public static function className()
{
return get_called_class();
}
定义于: yii\base\BaseObject::hasMethod()
返回一个值,指示是否定义了方法。
默认实现是调用 php 函数 method_exists()。当您实现 php 魔术方法 __call() 时,您可以覆盖此方法。
| public boolean hasMethod ( $name ) | ||
| $name | string |
方法名称 |
| 返回值 | boolean |
该方法是否已定义 |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定义于: yii\base\BaseObject::hasProperty()
返回一个值,指示是否定义了属性。
一个属性是已定义的,如果
- 该类有一个与指定名称关联的 getter 或 setter 方法(在这种情况下,属性名不区分大小写);
- 该类有一个与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参见
| public boolean hasProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
该属性是否已定义 |
|---|---|---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
| public void init ( ) |
public function init()
{
}
注册 或 登录 以发表评论。