rafaa1994
rafaa1994

Reputation: 71

Yii2 tableName return values

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';
}

Upvotes: 4

Views: 1197

Answers (1)

topher
topher

Reputation: 14860

'{{%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()), '_') . '}}';
}

Upvotes: 4

Related Questions