Welcome Guest, Not a member yet? Register   Sign In
Feature to not append database prefix in QueryBuilder
#3

(This post was last modified: 05-02-2025, 04:02 AM by objecttothis.)

@michalsn  Thank you for the tip on using forge to drop a foreign key, but it's missing the point.  The point is that builder doesn't have means of turning off the appending of the prefix but sometimes it's needed.  For example, if I extracted the builder code into a function foreignKeyExists(string $constraintName, string $tableName, string $databaseName): bool then the foreach becomes

PHP Code:
    foreach ($foreignKeys as $fk) {
        if(foreignKeyExists($fk$tableName$databaseName)) {
            $db->query("ALTER TABLE `$table` DROP FOREIGN KEY `$fk`"); //This could be changed to use forge, but the problem still exists
        }
    

Now my foreignKeyExists() function has the same problem and it has nothing to do with forge.
PHP Code:
fuction foreignKeyExists(string $constraintNamestring $tableNamestring $databaseName): bool {
        $builder $db->table('INFORMATION_SCHEMA.TABLE_CONSTRAINTS');
        $builder->select('CONSTRAINT_NAME');
        $builder->where('TABLE_SCHEMA'$databaseName);
        $builder->where('TABLE_NAME'$tableName);
        $builder->where('CONSTRAINT_TYPE''FOREIGN KEY');
        $builder->where('CONSTRAINT_NAME'$constraintName);
        $query $builder->get();

        return $query->getNumRows() > 0;
    

Adding the ability in table() to not append the prefix saves code and is useful when you don't want to append table names.
Reply


Messages In This Thread
RE: Feature to not append database prefix in QueryBuilder - by objecttothis - 05-02-2025, 03:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB