5

In an attempt to define a one-to-many relationship across bundles the following occurs:

The class 'Mana\ClientBundle\Entity\Member' was not found in the chain configured namespaces Mana\SplitBundle\Entity

Update 3:

I've now seen conflicting answers that the relationship can and cannot be accomplished. On the assumption that it can (because others here at stackoverflow seem to have done it), what configuration is required other than registering the bundles in AppKernel.php and entering the annotations in the entities? The resolve_target_entity_listener did not appear to make a difference.

Update 2:

Well, I know I'm way out of my depth here, but this is what I observed while stepping through the code when trying to show a Client entity.

The error message in the profiler

The target entity 'Mana\ClientBundle\Entity\Member' specified on Mana\SplitBundle\Entity\Client#members is unknown or not an entity.

occurs because SchemaValidator evaluates $cmf->isTransient($assoc['targetEntity']) to true, where the targetEntity in the Member entity. The PHPdoc comment suggests that this entity's metadata is not loaded. If I understand this correctly, that means that the annotation regarding the relationship is not loaded. But observing variable values suggests that the annotations have been read.

Am I totally missing something that should be painfully obvious? Or am I too far out in left field?

Update 1:

I have confirmed that doctrine:mapping:info will detect improper FQCN. The data fixtures are correct. Use of entity managers and database-connection for both default and split connections are correct. The error persists and can occur for any of the relationships defined in the Client entity, either OneToMany or ManyToOne.

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
            mapping_types: 
                enum:       string
          split:
            driver:   "%database_driver2%"
            host:     "%database_host2%"
            port:     "%database_port2%"
            dbname:   "%database_name2%"
            user:     "%database_user2%"
            password: "%database_password2%"
            charset:  UTF8
            mapping_types: 
                enum:       string
    entity_managers:
      default:
        connection: default
        mappings:
          ManaClientBundle: ~
      split:
        connection: split
        mappings:
          ManaSplitBundle: ~

Client entity:

/**
 * @ORM\OneToMany(targetEntity="Mana\ClientBundle\Entity\Member", mappedBy="client")
 * @ORM\OrderBy({"dob" = "ASC"})
 */
protected $members;

Member entity:

 /**
 * @ORM\ManyToOne(targetEntity="Mana\SplitBundle\Entity\Client",inversedBy="members",cascade={"remove", "persist"})
 * @ORM\JoinColumn(name="cid", referencedColumnName="id")
 * 
 */
 protected $client;

Doctrine mapping:

$ php app/console doctrine:mapping:info
Found 12 mapped entities:
[OK]   Mana\ClientBundle\Entity\Agency
[OK]   Mana\ClientBundle\Entity\Center
[OK]   Mana\ClientBundle\Entity\Contact
[OK]   Mana\ClientBundle\Entity\Contactdesc
[OK]   Mana\ClientBundle\Entity\Counties
[OK]   Mana\ClientBundle\Entity\Ethnicity
[OK]   Mana\ClientBundle\Entity\Incomehistory
[OK]   Mana\ClientBundle\Entity\Incomesrc
[OK]   Mana\ClientBundle\Entity\Member
[OK]   Mana\ClientBundle\Entity\Note
[OK]   Mana\ClientBundle\Entity\Referral
[OK]   Mana\ClientBundle\Entity\User

$ php app/console doctrine:mapping:info --em=split
Found 1 mapped entities:
[OK]   Mana\SplitBundle\Entity\Client
geoB
  • 4,578
  • 5
  • 37
  • 70
  • You should see http://stackoverflow.com/questions/11463517/using-relationships-with-multiple-entity-managers – Michael Sivolobov Apr 15 '13 at 02:19
  • @Michael Sivolobov: A cross-bundle relationship cannot be managed by Doctrine? Instead, in this case, the Client entity would have to reside in the same schema/bundle and be periodically refreshed from the external source? – geoB Apr 15 '13 at 12:51
  • On further investigation, doesn't the above reference conflict with [this answer](http://stackoverflow.com/questions/11774370/onetomany-relation-on-cross-project-entities-symfony2-doctrine) – geoB Apr 15 '13 at 17:20
  • It works only when you use one entity manager for both bundles. You must use only one connection. If you have separate databases with separate connections and entity managers It will not work. – Michael Sivolobov Apr 16 '13 at 03:10
  • @Michael Sivolobov: I'll accept your comment as an answer if it's posted as such. – geoB Apr 17 '13 at 00:59

1 Answers1

3

You should see Using Relationships with Multiple Entity Managers

A cross-bundle relationship cannot be managed by Doctrine if you have separate databases with separate connections and entity managers. Instead, in this case, the Client entity would have to reside in the same schema/bundle and be periodically refreshed from the external source.

But if you have only one connection to the database and one entity manager for it you can manage cross-bundle relationships. (Described here: OneToMany Relation on cross project entities (Symfony2/Doctrine))

Community
  • 1
  • 1
Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
  • Thanks for putting me on the right track. My ultimate solution has been to build a super-entity that contains both the foreign and local objects. From the outside it looks virtually identical to my original client entity. On the inside I maintain the properties of each. Once assembled, my code requires only minor tweaks to use the super entity. Both entities are persisted to their respective dbs. – geoB Apr 17 '13 at 13:47
  • @geoB Could you provide some more insight on how you solved this? (with code snippets if possible) Thanks in advance – Restless Aug 27 '14 at 11:15
  • @Restless: I was anticipating a problem that my client looked like they would present but eventually did not. So I never really had to solve the problem. – geoB Aug 27 '14 at 13:05
  • Oh, ok. Appreciate your reply though. I'll take a further look into this then. – Restless Aug 27 '14 at 13:39
  • @Restless what is your problem? – Michael Sivolobov Aug 27 '14 at 15:53
  • Hi @MichaelSivolobov I've finally managed to get this working using the insight from this answer and these other two: http://stackoverflow.com/a/17136583/563785 http://stackoverflow.com/a/22814389/563785 – Restless Oct 31 '14 at 13:52