Question

I'm trying out Fat Free Framework in an environment where I need to connect to an MSSQL database over PDO. I wrote some code to test out the database querying functionality. Here is my first attempt:

$f3->route('GET /datasets/read', function($f3) {
    $db = new DB\SQL('sqlsrv:Server=192.168.**.**;Database=test',"sa","*****");

    $results = $db->exec('SELECT * FROM builds');
    var_dump($results);
});

This works fine. I see a nice dump of all the records in the builds table. So far so good.

The next thing I tried was using the ORM approach provided by the framework:

$f3->route('GET /datasets/read', function($f3) {
    $db = new DB\SQL('sqlsrv:Server=192.168.**.**;Database=test',"sa","*****");

    $builds =new DB\SQL\Mapper($db,'builds')
    $builds->load();
});

This, however, fails. The generated query has no fields (this is the exact query generated: SELECT FROM builds), which results in:

Internal Server Error

PDOStatement: [Microsoft][SQL Server Native Client 11.0][SQL Server] Incorrect syntax near the keyword 'FROM':

[call stack]...[/call stack]

Am I doing something incorrectly here? I can't imagine there's a problem with the connection string, since it manages to retrieve data just fine when using the raw SQL approach. Is some sort of configuration needed to enable table to entity mapping?

I am following along with the guide provided here, for reference.

Était-ce utile?

La solution

I dug through the logs a bit, and discovered that the following query was being executed to retrieve the table schema:

SELECT c.column_name     AS field, 
       c.data_type       AS type, 
       c.column_default  AS defval, 
       c.is_nullable     AS nullable, 
       t.constraint_type AS pkey 
FROM   information_schema.columns AS c 
       LEFT OUTER JOIN information_schema.key_column_usage AS k 
                    ON c.table_name = k.table_name 
                       AND c.column_name = k.column_name 
                       AND c.table_schema = k.table_schema 
       LEFT OUTER JOIN information_schema.table_constraints AS t 
                    ON k.table_name = t.table_name 
                       AND k.constraint_name = t.constraint_name 
                       AND k.table_schema = t.table_schema 
WHERE  c.table_name = 'builds' 
       AND c.table_schema = 'test****';

The query was trying to enforce that the table_schema field contain the database name! No wonder it wasn't finding any columns. Going to lib/db/sql/sql.php and changing line 249 from this:

($this->engine=='pgsql'?
'c.table_catalog':'c.table_schema').

to this:

($this->engine=='pgsql'||$this->engine== 'sqlsrv'?
'c.table_catalog':'c.table_schema').

fixes the problem. I am not aware of how other DBs store the field indicating the database name, so I suppose it is best to be conservative and only make this change for SQL server databases, where I am sure it is needed.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top