简体   繁体   中英

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 :(

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM