Belita Colares
Belita Colares

Reputation: 79

Cross Database Joins Doctrine

I have 2 connection in my Doctrine with ZF2, but i need make join in twoo databases. The big question is: is it possible to perform a join on two tables which are in different databases and connections?

'connection' => array(
            // Default DB connection
           'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host' => 'localhost',
                    'user' => 'root',
                    'port' => '3306',
                    'password' => '',
                    'dbname' => 'MYSQL_TEST',
                    'driverOptions' => array(
                        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
                    )
                ),
            ),

            // Alternative DB connection
            'orm_alternative' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
                'params' => array(
                    'host' => 'localhost',
                    'user' => 'sa',
                    'port' => '',
                    'password' => 'test',
                    'dbname' => 'MSSQL_TEST',
                ),
            ),
        ),

Entity MYSQL Example:

   /**
     * mysql_test
     *
     * @ORM\Table(name="mysql_table")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     * @ORM\Entity(repositoryClass="MYSQL\Entity\TestRepository")
     */
    class Test
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;

         /**
         * @ORM\OneToOne(targetEntity="MSSQL\Entity\Test")
         * @ORM\JoinColumn(name="id_mssql", referencedColumnName="id_mssql")
         */
        private $mssql;

I would like to make a join between these two connections :(

Upvotes: 0

Views: 1104

Answers (1)

dnapierata
dnapierata

Reputation: 1213

Short answer you are probably looking for is, unfortunately, no.

There are ways for two separate connections (mysql and mssql in your case) to share data and this post might lead you down the right track if you are feeling extra adventurous.

But I would recommend just selecting your data from each connection separately and then manipulating it in php by matching common keys from each result to essentially emulate a join.

Upvotes: 2

Related Questions