Yii2 表名返回值

标签 yii2 yii2-model

你能告诉我函数 tableName() 和有什么区别吗?在 Yii2 中返回值 {{%table_name}} 和 'table_name' 的类中?

public static function tableName(){
return {{%admin}};
}



public static function tableName(){
return 'admin';
}

最佳答案

'{{%admin}}'将以 table prefix 为前缀如果设置了一个。 'admin'将不会。

我找不到确切的引用资料,但可以从 \yii\db\ActiveRecord::tableName() 的文档和源代码中推断出来。 .

Docs :

By default this method returns the class name as the table name by calling yii\helpers\Inflector::camel2id() with prefix yii\db\Connection::$tablePrefix. If yii\db\Connection::$tablePrefix is 'tbl_', 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes 'tbl_order_item'. You may override this method if the table is not named after this convention.



Source Code是:
public static function tableName()
{
    return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';
}

关于Yii2 表名返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548188/

相关文章:

Yii 2 根据连接表中的值过滤多对多关系

php - SQLSTATE[HY000] : General error in Yii2 , 创建命令

php - yii2 ActiveForm字段添加html代码

php - 如何在 joinWith() yii2 中获取基表列

php - 如何在 Yii2 搜索模型中使用数组进行搜索

mysql - 查询之间的 Yii2 模型搜索

activerecord - 预加载相关模型(嵌套集)

yii2 - 如何在 IIS 上正确配置 web.config 以发布 YII2 Advanced

mysql - Yii2 Sphinx 搜索大于小于或小于小于条件 OR "BETWEEN"

model-view-controller - Yii2 验证多个模型