2

I am trying to retrieve some data from tables which exist in two separate databases. Nothing seems to work - even the suggestions from the official documentation.

Let's say we have two tables in two databases:

db1 (default)
  - orders
db2
  - customers

If we set up the customers table correctly with a different connection name:

class CustomersTable extends Table
{

    public static function defaultConnectionName() {
        return 'db2';
    }

A normal contain will not work:

$this->Customers->find()
    ->contain(['Orders'])
    ->where(['Customers.id' => 12345]);

Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db2.orders' doesn't exist

Edit: This works in Cake 3.1 but not 3.3


The Cake documentation has a section about changing the fetching strategy to select, which would basically just run a separate query for the table in the other database (kind of a pointless solution for this anyway).

It doesn't seem to work either:

$this->Customers->find()
    ->contain([
        'Orders' => [
            'strategy' => 'select',
            'queryBuilder' => function ($q) {
                return $q->select(['field1', 'field2', 'field3']);
            }
        ]
    ])
    ->where(['Customers.id' => 12345]);

In this case, the result object of the query has an empty order key. What do I need to do to get this strategy to work?

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • Which 3.3 version exactly are you testing? 3.3.1 removed a feature that was causing problems with cross shema joins. Also what exactly do the queries look like that are being generated for the two variants? – ndm Jun 13 '17 at 12:31
  • Hi @ndm, I just upgraded from 3.1 to 3.3.5 I believe. In the first query for a normal contain, no tables have database prefixes so they are all assumed to be part of the database on which the connection is made. In the second query, I'm not sure what the query looks like. I already had to implement a fix with a workaround and don't have time to look at it more at the moment. – BadHorsie Jun 13 '17 at 12:36
  • That seems like a kinda weird update target, given that the latest 3.3 release is 3.3.16. Please check what version exactly you are using (last line in `vendor/cakephp/cakephp/VERSION.txt`). And what about the `select` strategy queries? – ndm Jun 13 '17 at 12:41
  • @ndm Definitely 3.3.5. I can't look at the select strategy query right now unfortunately. – BadHorsie Jun 13 '17 at 12:46
  • I don't see this working in 3.1 either. [**Last time I've checked**](https://stackoverflow.com/questions/32033558/how-to-use-different-datasources-in-a-query-using-cakephp3/32034850) there was no connection based cross-db support for joins, and on a quick look I couldn't find a change for that, other than **https://github.com/cakephp/cakephp/pull/8354**, which however has been retracted in 3.3.1 (after being introduced in 3.3.0) as mentioned earlier. So it feels like there's something missing here. – ndm Jun 13 '17 at 14:03

0 Answers0