Welcome Guest, Not a member yet? Register   Sign In
BaseConnection::$swapPre
#1

I don't understand the meaning of the swapPre property.
It works in conjunction with DBPrefix (replaces DBPrefix). It is configured in the config. It cannot be set/changed dynamically (there is no public method).
Can someone explain with a real example?
Reply
#2

Quote:A default table prefix that should be swapped with dbprefix. This is useful for distributed
applications where you might run manually written queries, and need the prefix to still be
customizable by the end user.
https://codeigniter.com/user_guide/datab...-of-values

In my understanding, when you have a query like "select * from foo_table", if you set
DBPrefix = 'db_' and swapPre = 'foo_', you can get the query "select * from db_table".
Reply
#3

(01-22-2022, 10:22 PM)kenjis Wrote: In my understanding, when you have a query like "select * from foo_table", if you set
DBPrefix = 'db_' and swapPre = 'foo_', you can get the query "select * from db_table".

It works like this.
PHP Code:
$db->table('table')->select('table.baz'); // SELECT table.baz FROM table
// DBPrefix = 'db_'  = (add prefix at sql compilation stage) -> SELECT db_table.baz FROM db_table
// DBPrefix = 'db_' + swapPre = 'foo_' =  (add prefix at sql compilation stage) SELECT db_table.baz FROM db_table -> SELECT foo_table.baz FROM foo_table 
For automatically added prefixes DBPrefix = 'db_' + swapPre = 'foo_' === DBPrefix = 'foo_'

But if we use static SQL
PHP Code:
$db->query('SELECT db_table.baz FROM db_table');
// DBPrefix = 'db_' + swapPre = 'foo_' = SELECT db_table.baz FROM db_table -> SELECT foo_table.baz FROM foo_table 
But it also affects her other requests.

Configs is an entity that rarely changes.
From this point of view, it is easier and more efficient to change the static SQL.

In my opinion, this transformation is meaningless. At least I did not find a real need for this functionality.
So I want to know a real use case.
Reply
#4

When you have an app with using the query() method and having prefixed (`db_`) table names,
You have a query like `SELECT db_table.baz FROM db_table`.
And you or someone wants to use the app with the different environment that has the different DB prefix `foo_`,
DBPrefix and swapPre makes the query `SELECT foo_table.baz FROM foo_table`.


Quote:This is useful for distributed
applications where you might run manually written queries, and need the prefix to still be
customizable by the end user.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB