0

I try to develop a website using Symfony 2. I want to use 2 databases. One with general data (DB_DATA) and another with my application data (DB_APP). I've made two bundles "MyCompany\DataBundle" and "MyCompany\AppBundle".

In "MyCompany\DataBundle" I've declared an entity "Customer" which referes to the clients of my company. In "MyCompany\AppBundle" there is an Entity "Account" which is used by the application and must reference the Customer.

For now they look like this :

\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

Everytime I try to update schema, I get this error :

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

T00rk
  • 2,178
  • 3
  • 17
  • 23

1 Answers1

0

Look at this line:

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

try without first "\", because it isn't in "MyCompany\AppBundle\Entity" namespace\folder:

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

If path is relative it is assumed to be relative to the bundle root.

Edited: After comment disuss: (i mean this)... in my proj im using YAML, so my descriptions are shorter like this:

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

In your case it'll look like this (not sure in syntax):

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: Its still not working... and seems won't work after i read this

derkien
  • 66
  • 3
  • Ok, try to configure this file path\app\config\config.yml Somewhere after this line: `# Doctrine Configuration` `doctrine:` describe your orm, like this: `orm: default_entity_manager: default default_entity_manager: default auto_generate_proxy_classes: "%kernel.debug%" entity_managers: default: connection: nameofyourconnection mappings: AppBundle: ~ DataBundle: ~ ` for more info look [this answer](http://stackoverflow.com/a/22814389/2776257) – derkien Nov 07 '14 at 07:32
  • I've already done that in my config. I've edited my post to show what my `config.yml`looks like – T00rk Nov 07 '14 at 10:02
  • There is still not the same that i mean. You defined mappings to your bundles separately, that means that bundle will look for enties links inside AppBundle only, or inside DataBundle only for different orm connections. You have to define in 1. entity_managers->default->mappings: and 2. entity_managers->app->mappings: that relationships may be in distinct bundles, copy info about both bundles (AppBundle, DataBundle) in 1. and 2. I have the same thing in my proj code and it works fine (but im using YAML) – derkien Nov 07 '14 at 11:09
  • The good news is I don't have the exception anymore. the bad one is know when I update schema using console it creates my all the tables in every DB. :/ – T00rk Nov 07 '14 at 11:36
  • I have the same... It seems i was wrong (actually im doing same thing right now and trying to help you with my progress)... Look [what i found](https://github.com/doctrine/DoctrineBundle/issues/60) last posts 1-4 october... – derkien Nov 07 '14 at 12:37
  • I've found this that could probably help http://stackoverflow.com/questions/13648974/symfony2-doctrine2-cross-database-join-column-throws-mapping-exception – T00rk Nov 07 '14 at 12:48
  • Its not exactly what we need. Its just another complex way to map id's between bundles. You also can register a service with method, that takes entity from bundleOne and make something special with it in bundleTwo and call this method everytime you need to make "link" between two bundle's Entities (e.g. save Element in db1 - elementBundle after that send that element to db2 historyBundle's method, wich makes a history record in db2). But its not really Relationship... – derkien Nov 07 '14 at 13:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64496/discussion-between-derkien-and-t00rk). – derkien Nov 07 '14 at 13:11