类 yii\db\pgsql\ColumnSchema
| 继承 | yii\db\pgsql\ColumnSchema » yii\db\ColumnSchema » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable |
| 源代码 | https://github.com/yiisoft/yii2/blob/master/framework/db/pgsql/ColumnSchema.php |
用于 PostgreSQL 数据库的 ColumnSchema 类。
公共属性
公共方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| __call() | 调用不是类方法的命名方法。 | yii\base\BaseObject |
| __construct() | 构造函数。 | yii\base\BaseObject |
| __get() | 返回对象属性的值。 | yii\base\BaseObject |
| __isset() | 检查属性是否已设置,即已定义且不为空。 | yii\base\BaseObject |
| __set() | 设置对象属性的值。 | yii\base\BaseObject |
| __unset() | 将对象属性设置为 null。 | yii\base\BaseObject |
| canGetProperty() | 返回一个值,指示是否可以读取属性。 | yii\base\BaseObject |
| canSetProperty() | 返回一个值,指示是否可以设置属性。 | yii\base\BaseObject |
| className() | 返回此类的完全限定名称。 | yii\base\BaseObject |
| dbTypecast() | 根据 $type 和 $dbType 转换输入值,以便在数据库查询中使用。 | yii\db\pgsql\ColumnSchema |
| hasMethod() | 返回一个值,指示方法是否已定义。 | yii\base\BaseObject |
| hasProperty() | 返回一个值,指示属性是否已定义。 | yii\base\BaseObject |
| init() | 初始化对象。 | yii\base\BaseObject |
| phpTypecast() | 从数据库检索后,根据 $phpType 转换输入值。 | yii\db\pgsql\ColumnSchema |
受保护的方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| getArrayParser() | 创建 ArrayParser 的实例 | yii\db\pgsql\ColumnSchema |
| phpTypecastValue() | 从 DBMS 检索后将 $value 转换为 PHP 表示形式。 | yii\db\pgsql\ColumnSchema |
| typecast() | 从数据库检索后,根据 $phpType 转换输入值。 | yii\db\ColumnSchema |
属性详细信息
是否将数组列值反序列化为 yii\db\ArrayExpression 对象。您可以使用此属性使升级到 Yii 2.0.14 更轻松。默认为 true,这意味着数组将反序列化为 yii\db\ArrayExpression 对象。
是否应省略使用 PgSQL 数组支持功能的列模式。您可以使用此属性使升级到 Yii 2.0.14 更轻松。默认为 false,表示启用数组支持。
是否应省略使用 JSON 支持功能的列模式。您可以使用此属性使升级到 Yii 2.0.14 更容易。默认值为 false,这意味着 JSON 支持已启用。
方法详情
| 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()");
}
定义于: yii\base\BaseObject::__construct()
构造函数。
默认实现执行两件事
- 使用给定的配置
$config初始化对象。 - 调用 init().
如果此方法在子类中被重写,建议
- 构造函数的最后一个参数是一个配置数组,如这里的
$config。 - 在构造函数的末尾调用父级实现。
| public void __construct ( $config = [] ) | ||
| $config | array |
将用于初始化对象属性的键值对 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定义于: 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 |
命名的属性是否已设置(不为 null)。 |
|---|---|---|
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);
}
}
定义于: 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();
}
根据 $type 和 $dbType 转换输入值,以便在数据库查询中使用。
如果值为 null 或 yii\db\Expression,则不会进行转换。
| public 混合 dbTypecast ( $value ) | ||
| $value | mixed |
输入值 |
| 返回值 | mixed |
转换后的值。这可能也是一个数组,其中第一个元素是值,第二个元素是 PDO 类型。 |
|---|---|---|
public function dbTypecast($value)
{
if ($value === null) {
return $value;
}
if ($value instanceof ExpressionInterface) {
return $value;
}
if ($this->dimension > 0) {
return $this->disableArraySupport
? (string) $value
: new ArrayExpression($value, $this->dbType, $this->dimension);
}
if (!$this->disableJsonSupport && in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
return new JsonExpression($value, $this->dbType);
}
return $this->typecast($value);
}
创建 ArrayParser 的实例
| protected yii\db\pgsql\ArrayParser getArrayParser ( ) |
protected function getArrayParser()
{
static $parser = null;
if ($parser === null) {
$parser = new ArrayParser();
}
return $parser;
}
定义于: yii\base\BaseObject::hasMethod()
返回一个值,指示方法是否已定义。
默认实现是调用 PHP 函数 method_exists()。当您实现 PHP 魔术方法 __call() 时,可以覆盖此方法。
| public 布尔值 hasMethod ( $name ) | ||
| $name | string |
方法名 |
| 返回值 | boolean |
方法是否已定义 |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定义于: yii\base\BaseObject::hasProperty()
返回一个值,指示属性是否已定义。
属性定义如下:
- 类具有与指定名称关联的 getter 或 setter 方法(在这种情况下,属性名称不区分大小写);
- 该类具有与指定名称相匹配的成员变量(当
$checkVars为 true 时);
另请参阅
| public 布尔值 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 空 init ( ) |
public function init()
{
}
从数据库检索后,根据 $phpType 转换输入值。
如果值为 null 或 yii\db\Expression,则不会进行转换。
| public 混合 phpTypecast ( $value ) | ||
| $value | mixed |
输入值 |
| 返回值 | mixed |
转换后的值 |
|---|---|---|
public function phpTypecast($value)
{
if ($this->dimension > 0) {
if ($this->disableArraySupport) {
return $value;
}
if (!is_array($value)) {
$value = $this->getArrayParser()->parse($value);
}
if (is_array($value)) {
array_walk_recursive($value, function (&$val, $key) {
$val = $this->phpTypecastValue($val);
});
} elseif ($value === null) {
return null;
}
return $this->deserializeArrayColumnToArrayExpression
? new ArrayExpression($value, $this->dbType, $this->dimension)
: $value;
}
return $this->phpTypecastValue($value);
}
从 DBMS 检索后将 $value 转换为 PHP 表示形式。
| protected 布尔值|混合|null phpTypecastValue ( $value ) | ||
| $value | 字符串|null | |
protected function phpTypecastValue($value)
{
if ($value === null) {
return null;
}
switch ($this->type) {
case Schema::TYPE_BOOLEAN:
switch (strtolower($value)) {
case 't':
case 'true':
return true;
case 'f':
case 'false':
return false;
}
return (bool) $value;
case Schema::TYPE_JSON:
return $this->disableJsonSupport ? $value : json_decode($value, true);
}
return parent::phpTypecast($value);
}
定义于: yii\db\ColumnSchema::typecast()
从数据库检索后,根据 $phpType 转换输入值。
如果值为 null 或 yii\db\Expression,则不会进行转换。
| protected 混合 typecast ( $value ) | ||
| $value | mixed |
输入值 |
| 返回值 | mixed |
转换后的值 |
|---|---|---|
protected function typecast($value)
{
if (
$value === ''
&& !in_array(
$this->type,
[
Schema::TYPE_TEXT,
Schema::TYPE_STRING,
Schema::TYPE_BINARY,
Schema::TYPE_CHAR
],
true
)
) {
return null;
}
if (
$value === null
|| gettype($value) === $this->phpType
|| $value instanceof ExpressionInterface
|| $value instanceof Query
) {
return $value;
}
if (
is_array($value)
&& count($value) === 2
&& isset($value[1])
&& in_array($value[1], $this->getPdoParamTypes(), true)
) {
return new PdoValue($value[0], $value[1]);
}
switch ($this->phpType) {
case 'resource':
case 'string':
if (is_resource($value)) {
return $value;
}
if (is_float($value)) {
// ensure type cast always has . as decimal separator in all locales
return StringHelper::floatToString($value);
}
if (
is_numeric($value)
&& ColumnSchemaBuilder::CATEGORY_NUMERIC === ColumnSchemaBuilder::$typeCategoryMap[$this->type]
) {
// https://github.com/yiisoft/yii2/issues/14663
return $value;
}
if (PHP_VERSION_ID >= 80100 && is_object($value) && $value instanceof \BackedEnum) {
return (string) $value->value;
}
return (string) $value;
case 'integer':
if (PHP_VERSION_ID >= 80100 && is_object($value) && $value instanceof \BackedEnum) {
return (int) $value->value;
}
return (int) $value;
case 'boolean':
// treating a 0 bit value as false too
// https://github.com/yiisoft/yii2/issues/9006
return (bool) $value && $value !== "\0" && strtolower($value) !== 'false';
case 'double':
return (float) $value;
}
return $value;
}
注册 或 登录 才能评论。