I have two databases: the local (default) database used for the OCMS installation and a external database for sensitive data. I’ve created modals for the external database using $connection, however, how do I create relations between the local & external modals?
I’ve tried (in the modal using the external database):
public $hasMany = [
'users' => ['RainLab\User\Models\User'],
but get an error: Invalid object name ‘users’.
In most cases, relations between models on different connections work exactly the same as if they were on the same connection. So your error is very likely not because you have different connections, but you have some other bug in the code.
The model connected to the external database was created in Builder. The error occurs when I’m trying to access the controller of the model connected to the external database in the backend. If I make a duplicate of the external table on the local server, it works fine. For some reason it seems to search for the local (RainLab) users table on the external server instead of the local server. As mentioned, the installation is on the local server, sensitive data on the external server, but linked to users.
The problem with multi-connection relations is the deferred binding table will not exist or contain valid entries. So it is essential to disable deferred binding across databases.