leftJoin two different tables in different databases
leftJoin two different tables in different databases
Hello everyone :)
i just searched for a while to solve this problem but no success.
Here my code:
config.php
$sql_details = array(
"type" => ..
"user" => ..
"pass" => ..
"host" => ..
"port" =>..
"db" => "anfragenmanagement",
"dsn" => ..
);
staff.php
Editor::inst( $db, 'anfragen' )
->fields(
Field::inst( 'anfragen.AnfrageID' ),
Field::inst( 'anfragen.Kunde' ),
Field::inst( 'anfragen.Ansprechpartner' ),
Field::inst( 'anfragen.KAPMitarbeiter' ),
Field::inst( 'anfragen.Beschreibung' ),
Field::inst( 'anfragen.Datum' ),
Field::inst( 'anfragen.Status' ),
Field::inst( 'anfragen_status.ID' )
)
->leftJoin( 'anfragen_status', 'anfragen_status.Status', '=', 'anfragen.Status' )
->process( $_POST )
->json();
It works fine but in addition i want to leftJoin this "anfragenmanagement.anfragen" table with a table of an other database called "projektdatenbank.members" . Both databases have the same sql configuration.
I can connect to another database through the config.php and Bootstrap.php but i dont know how to put this information into the staff.php. I hope someone can help :)
Thanks. Jenni
Answers
Hi Jenni,
Thanks for your question. It should just be a case of prefixing the database names in front of the table names, in exactly the same way as you have prefixed the table names in front of the fields. For example
db1.anfragen
for the Editor instance anddb1.anfragen.AnfrageID
for the fields, etc.Allan
Hi allan,
thanks for your fast answer. :)
Is it right that i can just have one database connection for each editor instance?
In the code above i have $db as one database connection and access the table "anfragen" in this database. So when i want a second connection to another database i need to create a second editor instance or is it possible to access two databases in one editor instance?
I can create a second connection like this:
and create with this connection details in the Bootstrap.php a variable $db2.
Can i access $db and $db2 in one editor instance?
Thanks
Jenni
Yes, however, it could have access to multiple databases if the SQL server is set up that way. The
db
property is just the default database.Allan
Hmm. I understand what you are writing but i'm very confused how i can access the tables of $db2 in this editor instance.
i tried this:
OR
but it just will not work :(
When i have a second database connection in $db2 , how can i access the tables and fields?
Many thanks in advance
Jenni
Just prefix the database name in front of all the tables names like you would in an SQL query.
Allan
Hey allan,
i unterstand it but in your presented code, how can i access for example database2Name.members?
Thanks in advance
Jenni
Just exactly as you have done. With the database name prefixed. If that isn't working for you can you show me your full PHP and a link to the page you are working on please.
Allan