4

Could you tell me what is the difference between function tableName() in class which return value {{%table_name}} and 'table_name' in Yii2 ??

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



public static function tableName(){
return 'admin';
}
GAMITG
  • 3,810
  • 7
  • 32
  • 51
rafaa1994
  • 71
  • 3

1 Answers1

4

'{{%admin}}' will be prefixed with the table prefix if one is set. 'admin' will not.

I can't find a reference for this exactly but it can be inferred from the docs and source code for \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 is:

public static function tableName()
{
    return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';
}
topher
  • 14,790
  • 7
  • 54
  • 70