Issues in entities from different bundles using different entity managers

纵饮孤独 提交于 2019-12-12 03:19:32

问题


Edit:

I've prepared a tar.gz which once uncompressed and after running ./bin/vendors install fails to load the fixtures through php scripts/createAll.php. In the tar.gz there are 2 bundles using 2 different connections everyone with its own database.

I think Symfony2 fails to manage them properly. If you take a look at scripts/createAll.php will see how symfony fails to load both fixtures, but if you remove a random fixture (it doesn't matter Var_.php or Foo_.php everything runs fine, which seems to me that symfony is failing to manage entities correctly.)

LINK: http://www.2shared.com/file/2u4GhFVX/SymfonyTestCasetar.html

i want to tell Symfony2 to use different entity managers for different Bundle directories so my config.yml looks like:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    default_entity_manager:   default
    entity_managers:
        default:
            connection: default
            mappings:
                myVendorURLCoreBundle: ~
                myVendormyBundleBundle: ~
                myVendormyBundleFooBundle:
                    prefix: "myVendor\myBundleFooBundle\Entity"
                    type: annotation
                    is_bundle: true
                    dir: "/Entity"
        formacions:
            connection: formacions
            mappings:
                myVendormyBundleFooBarBundle:
                    prefix: "myVendor\myBundleFooBarBundle\View"
                    type: annotation
                    is_bundle: false
                    dir: "%kernel.root_dir%/../src/myVendor/myBundleFooBarBundle/View"

The problem is when using relationships between the entities in the different directories i get the following error caused by vendor/doctrine/lib/Doctrine/ORM/Mapping/MappingException.php at line 142

Class FRJPC\SalleUrlFormacionsBundle\Entity\EspecialitatContingut is not a valid entity or mapped super class

The probem is that sometimes "\" before the vendor name breaks the namespace. It's really strange.

Here is how i i link entities between each other:



namespace myVendor\myBundleFooBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity( repositoryClass="myVendor\myBundleFooBundle\Repository\ARepository" )
 * @ORM\ChangeTrackingPolicy( "DEFERRED_EXPLICIT" )
 * @ORM\Table( name="a" )
 */
class A
{
    /**
     * @ORM\Id
     * @ORM\Column( type="integer", length="4" )
     * @ORM\GeneratedValue( strategy="AUTO" )
     */
    private $id;

    /** 
     * @ORM\ManyToOne( targetEntity="\myVendor\myBundleFooBarBundle\Entity\B", inversedBy="a", cascade={"persist"} )
     * @ORM\JoinColumn( name="FooBar", nullable=true, referencedColumnName="FooBar", onDelete="CASCADE" )
     */
    private $fooBar;
}

Second entity:


namespace myVendor\myBundleFooBarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity( repositoryClass="myVendor\myBundleFooBarBundle\Repository\ARepository" )
 * @ORM\ChangeTrackingPolicy( "DEFERRED_EXPLICIT" )
 * @ORM\Table( name="a" )
 */
class B
{
    /**
     * @ORM\Id
     * @ORM\Column( type="integer", length="4" )
     * @ORM\GeneratedValue( strategy="AUTO" )
     */
    private $id;

        /** @ORM\OneToMany( targetEntity="\myVendor\myBundleFooBundle\Entity\EspecialitatContingut", mappedBy="fooBar" ) */
        private $a;
}

Does any one has a clue on how should i link each entity ?

PD: Both entities work like charm when they're in the same bundle and same directory.


回答1:


All these Foos and Bars combined with an error message with a real name make it a bit difficult to follow and leaves open the possibility that posted code really doesn't match the actual code. The FooBarBundle/View seems to be an unlikely place to store entities.

In any event a given entity manager such as formacions needs to be able to see all the relevant entities including those involved in relations. It looks like you defined A in the foo bundle and B in the bar bundle and they both cross reference each other?

From your config, I don't see how the formacion em can see your A entity and likewise I don't see how the default em can see the B entity. The fully qualified class name in the relation is not enough to share the entity doctrine metadata. Hence the error messages.

I'd would actually be glad to be proven wrong about this. It's a bit frustrating not to be able to do these sorts of things.



来源:https://stackoverflow.com/questions/9330018/issues-in-entities-from-different-bundles-using-different-entity-managers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!