dongyao2022 2018-01-02 07:31
浏览 7
已采纳

too long

How can I implement this code in doctrine 2:

 Doctrine_Manager::connection('mysql://root@localhost/doctrine_test1', 'doctrine_test1');
Doctrine_Manager::connection('mysql://root@localhost/doctrine_test2', 'doctrine_test2');

any alternate. I am using this code for connection:

<?php
// bootstrap.php
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array("/path/to/entity-files");
$isDevMode = false;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'foo',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

example scenario:

--databse_a
CREATE TABLE User (
    id INT AUTO_INCREMENT NOT NULL,
    address_id INT DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

--databse_b
CREATE TABLE Address (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

ALTER TABLE database_a.User ADD FOREIGN KEY (address_id) REFERENCES database_b.Address(id);

Generate Entities and then try to fech results. You will get errors.

error in log: The target-entity Address cannot be found in 'User#address_id'.

  • 写回答

1条回答 默认 最新

  • duanchi0897 2018-03-03 06:28
    关注

    You need to create a connection with the user which has authority to access multiple databases like db1 and db2 both.

    Then you will just make some changes in your generated entity files. You will add database name before table name like:

    use Doctrine\ORM\Mapping as ORM;
    /**
     * User
     *
     * @ORM\Table(name="database_a.User", indexes={@ORM\Index(name="id", columns={"address_id"}), @ORM\Index(name="address_id", columns={"address_id"})})
     * @ORM\Entity
    */
    class User
    {
    

    You can download running example code from github

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?