2014-11-06 120 views
0

我嘗試使用Symfony 2開發一個網站。 我想使用2個數據庫。一個使用通用數據(DB_DATA),另一個使用我的應用程序數據(DB_APP)。 我製作了兩個捆綁包「MyCompany \ DataBundle」和「MyCompany \ AppBundle」。Symfony 2實體間關係組合

在「MyCompany \ DataBundle」中我宣佈了一個實體「客戶」,它反映了我公司的客戶。 在「我的公司\ AppBundle」中有一個實體「賬戶」,供應用程序使用,並且必須引用客戶。

現在他們看起來是這樣的:

\src\MyCompany\DataBundle\Entity\Customer.php

<?php 

namespace MyCompany\DataBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Customer 
* 
* @ORM\Table(name="customer") 
* @ORM\Entity 
*/ 
class Customer 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="ID", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="LASTNAME", type="string", length=255, nullable=false) 
    */ 
    private $lastName; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="FIRSTNAME", type="string", length=255, nullable=false) 
    */ 
    private $firstName; 

    ... 
} 

\src\MyCompany\AppBundle\Entity\Account.php

<?php 

namespace MyCompany\AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Account 
* 
* @ORM\Table(name="account") 
* @ORM\Entity 
*/ 
class Account 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="ID", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\OneToOne(targetEntity="\MyCompany\DataBundle\Entity\Customer") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="CUSTOMER_ID", referencedColumnName="ID") 
    * }) 
    */ 
    private $customer; 
} 

\app\config.yml

doctrine: 
    dbal: 
     default_connection: default 
     connections: 
      default: 
       driver: "%database_driver%" 
       host:  "%database_host%" 
       port:  "%database_port%" 
       dbname: "%database_name%" 
       user:  "%database_user%" 
       password: "%database_password%" 
       charset: UTF8 
      appdb: 
       driver: "%database_driver2%" 
       host:  "%database_host2%" 
       port:  "%database_port2%" 
       dbname: "%database_name2%" 
       user:  "%database_user2%" 
       password: "%database_password2%" 
       charset: UTF8 
    orm: 
     auto_generate_proxy_classes: %kernel.debug% 
     default_entity_manager: default 
     entity_managers: 
      default: 
       connection: default 
       mappings: 
        AppBundle: 
         type: annotation 
         mapping: true 
         dir: "%kernel.root_dir%/../src/MyCompany/AppBundle/Entity/" 
         prefix: MyCompany\AppBundle\Entity\ 
         is_bundle: false 
         alias: APP 
      app: 
       connection: appdb 
       mappings: 
        DataBundle: 
         type: annotation 
         mapping: true 
         dir: "%kernel.root_dir%/../src/MyCompany/DataBundle/Entity/" 
         prefix: MyCompany\DataBundle\Entity\ 
         is_bundle: false 
         alias: DATA 

每次我嘗試更新的模式,我得到這個錯誤:

[Doctrine\Common\Persistence\Mapping\MappingException] The class 'MyCompany\DataBundle\Entity\Customer' was not found in the chain configured namespaces MyCompany\AppBundle\Entity

回答

0

看這句話:

@ORM\OneToOne(targetEntity="\MyCompany\DataBundle\Entity\Customer") 

嘗試不第一個 「\」,因爲它不是在「MyCompany的\的appbundle \實體」命名空間\文件夾中:

@ORM\OneToOne(targetEntity="MyCompany\DataBundle\Entity\Customer") 

如果路徑是相對的它被認爲是相對於包根。

編輯: 評論disuss後: (我的意思是)......使用YAML我的凸出IM,所以我的描述是這樣的短:

orm: 
    default_entity_manager: default 
    auto_generate_proxy_classes: "%kernel.debug%" 
    entity_managers: 
     default: 
      connection: default 
      mappings: 
       otherbundles: ~ 
    #Kernel links to history 
       KernelBundle: ~ 
       HistoryBundle: ~ 
     history: 
      connection: history 
      mappings: 
    #History links to kernel 
       HistoryBundle: ~ 
       KernelBundle: ~ 

在你的情況下,它會像這樣(在語法上不知道):

orm: 
    auto_generate_proxy_classes: %kernel.debug% 
    default_entity_manager: default 
    entity_managers: 
     default: 
      connection: default 
      mappings: 
       AppBundle: 
        type: annotation 
        mapping: true 
        dir: "%kernel.root_dir%/../src/MyCompany/AppBundle/Entity/" 
        prefix: MyCompany\AppBundle\Entity\ 
        is_bundle: false 
        alias: APP 
       DataBundle: 
        type: annotation 
        mapping: true 
        dir: "%kernel.root_dir%/../src/MyCompany/DataBundle/Entity/" 
        prefix: MyCompany\DataBundle\Entity\ 
        is_bundle: false 
        alias: DATA 
     app: 
      connection: appdb 
      mappings: 
       DataBundle: 
        type: annotation 
        mapping: true 
        dir: "%kernel.root_dir%/../src/MyCompany/DataBundle/Entity/" 
        prefix: MyCompany\DataBundle\Entity\ 
        is_bundle: false 
        alias: DATA 
       AppBundle: 
        type: annotation 
        mapping: true 
        dir: "%kernel.root_dir%/../src/MyCompany/AppBundle/Entity/" 
        prefix: MyCompany\AppBundle\Entity\ 
        is_bundle: false 
        alias: APP 

UPD:它仍然沒有工作......,似乎是行不通的,我讀後

+0

我試過,但還是同樣的錯誤 – T00rk 2014-11-06 15:17:23

+0

好吧,嘗試配置文件路徑\程序\ CONFIG \ config.yml 某處此行之後: '#主義Configuration' '學說:' 描述你的ORM,像這樣: 'ORM: default_entity_manager:默認 default_entity_manager:默認 auto_generate_proxy_classes: 「%kernel.debug%」 entity_managers: 默認: 連接:nameofyourconnection 映射: 的appbundle:〜 DataBundle:〜 ' 有關更多信息,請查看[此答案](http://stackoverflow.com/a/22814389/2776257) – derkien 2014-11-07 07:32:15

+0

我已經在我的配置中完成了這項工作。我編輯了我的帖子,以顯示我的'config.yml'看起來像 – T00rk 2014-11-07 10:02:04