你能告诉我函数 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 prefixyii\db\Connection::$tablePrefix
. Ifyii\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/