2

I browsed the yii2-oauth2-server code on github and encountered interesting syntax in the migration file. The code in question looks like this:

    $this->createTable('{{%oauth_clients}}', [
        ...
    ], $tableOptions);

Can someone explain the {{%oauth_clients}} part? What does it mean when the table name is enclosed in {{%..}}? The official documentation on migrations says nothing about it and all examples have the "raw" table names like this:

    $this->createTable('post_tag', [
        ...
    ]);

I checked my database and the table was created as if there were no brackets, its name is simply oauth_clients.

Ruslan Bes
  • 2,715
  • 2
  • 25
  • 32
  • I might be wrong but I suspect it's to do with table prefixes eg: app_oauth_clients – Brett Gregson Aug 01 '16 at 11:51
  • 1
    @eskimo it seems you are right, I've found [another question](http://stackoverflow.com/questions/28998385/yii2-gii-table-prefix) about Gii and the same syntax was used in the ActiveRecord class, seems logical. – Ruslan Bes Aug 01 '16 at 11:57

1 Answers1

4

Is used for table prefix

From yii2 documentation on Db connection:

If a table name is given as {{%TableName}}, then the percentage character % will be replaced with this property value. For example, {{%post}} becomes {{tbl_post}}.

Ruslan Bes
  • 2,715
  • 2
  • 25
  • 32
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • So what about the braces? The % is only one of the strange things about it, and the documentation is completely silent about the `{{` and `}}`. – Throw Away Account Mar 26 '19 at 03:17
  • @ThrowAwayAccount the sample is obtained form the yii2 DB connection doc ..https://www.yiiframework.com/doc/api/2.0/yii-db-connection#$tablePrefix-detail – ScaisEdge Mar 26 '19 at 06:08
  • Curly brakets must be there to enable proper quoting of table names to avoid naming conflict e.g. with reserved keywords. This is because database systems support different quotes, e.g. mySQL/MariaDB uses backticks `\`table\`` or MSSQL square brackets `[table]` etc. – lubosdz Apr 04 '20 at 08:54