Symfony 3 / Bundle中的多个Doctrine EM

时间:2017-12-29 08:41:22

标签: symfony doctrine-orm doctrine

我一直在关注本教程https://symfony.com/doc/3.3/doctrine/multiple_entity_managers.html(但不仅仅是针对不同Bundles上的不同连接/ em),然后是网络上的一些链接,尝试在不同的数据库中获取不同的实体,在同一个包中,相互交织,我尝试了一切,但似乎没有什么工作......

起初我认为我的所有实体都在ModelBundle / Entity /文件夹中的事实是问题,所以我实际上移动了/ ModelBundle / EntityInternal /中的所有默认实体以及我要移动到另一个实体的其他实体/ ModelBundle / EntityRest /

中的数据库

(目前我只想尝试使用2个独立的mysql数据库,但之后的想法是使用CircleOfNice / DoctrineRestDriver作为辅助EM)

这是我目前的配置:

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver:   pdo_mysql
            host:     '%database_host%'
            port:     '%database_port%'
            dbname:   '%database_name%'
            user:     '%database_user%'
            password: '%database_password%'
            charset:  UTF8
            server_version: '5.6'
        secondary:
            driver:   pdo_mysql
            host:     '%database_host2%'
            port:     '%database_port2%'
            dbname:   '%database_name2%'
            user:     '%database_user2%'
            password: '%database_password2%'
            charset:  UTF8
            server_version: '5.6'

orm:
    #auto_generate_proxy_classes: '%kernel.debug%'
    #naming_strategy: doctrine.orm.naming_strategy.underscore
    #auto_mapping: true
    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                ModelBundle:
                    type: "annotation"
                    dir: "EntityInternal"
                    prefix: "ModelBundle\\EntityInternal"
        secondary:
            connection: secondary
            mappings:
                ModelBundle:
                    type: "annotation"
                    dir: "EntityRest"
                    prefix: "ModelBundle\\EntityRest"

我尝试在辅助连接/ EM中移动的第一个实体:

namespace ModelBundle\EntityRest;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

use ModelBundle\Model\ModelTrait\SeoTrait;
use ModelBundle\Model\ModelTrait\NameTrait;

/**
 * ModelBundle\EntityRest\Level2
 *
 * @ORM\Table(name="level2")
 * @ORM\Entity(repositoryClass="ModelBundle\Repository\LevelRepository")
 */
class Level2
{
    use SeoTrait;
    use NameTrait;

    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="\ModelBundle\EntityInternal\Offer", mappedBy="levels2")
     * @ORM\JoinColumn(name="offer_id", referencedColumnName="id", nullable=true)
     */
    private $offers;

    /**
     * @ORM\ManyToMany(targetEntity="\ModelBundle\EntityInternal\User", mappedBy="levels2")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
     */
    private $users;

以及保留在默认EM / Connection

中的链接实体
/**
 * ModelBundle\EntityInternal\Offer
 *
 * @ORM\Table(name="offer")
 * @ORM\Entity(repositoryClass="ModelBundle\Repository\OfferRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Offer
{
    use SeoTrait;
    use DateTrait;
    use ValidTrait;
    use PremiumTrait;
    use NameTrait;

    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    (....)

    /**
     * @ORM\ManyToMany(targetEntity="Level", inversedBy="offers")
     * @ORM\JoinColumn(nullable=false)
     */
    private $levels;

    /**
     * @ORM\ManyToMany(targetEntity="\ModelBundle\EntityRest\Level2", inversedBy="offers")
     * @ORM\JoinColumn(nullable=false)
     */
    private $levels2;

学说:mapping:info输出正确的实体:

$ bin/console doctrine:mapping:info --em secondary
Found 1 mapped entities:
[OK]   ModelBundle\EntityRest\Level2

$ bin/console doctrine:mapping:info
Found 17 mapped entities:
[OK]   ModelBundle\EntityInternal\Domain
[OK]   ModelBundle\EntityInternal\Application
[OK]   ModelBundle\EntityInternal\Region
[OK]   ModelBundle\EntityInternal\Offer
[OK]   ModelBundle\EntityInternal\Diploma
[OK]   ModelBundle\EntityInternal\ArticleCategory
[OK]   ModelBundle\EntityInternal\City
[OK]   ModelBundle\EntityInternal\County
[OK]   ModelBundle\EntityInternal\User
[OK]   ModelBundle\EntityInternal\Sector
[OK]   ModelBundle\EntityInternal\Level
[OK]   ModelBundle\EntityInternal\Company
[OK]   ModelBundle\EntityInternal\SectorGroup
[OK]   ModelBundle\EntityInternal\DomainGroup
[OK]   ModelBundle\EntityInternal\Media
[OK]   ModelBundle\EntityInternal\Article
[OK]   ModelBundle\EntityInternal\Contract

但架构:更新只是不想工作

$ bin/console doctrine:schema:update --em secondary

  [Doctrine\Common\Persistence\Mapping\MappingException]                                                                
  The class 'ModelBundle\EntityInternal\Offer' was not found in the chain configured namespaces ModelBundle\EntityRest  


$ bin/console doctrine:schema:update


  [Doctrine\Common\Persistence\Mapping\MappingException]                                                                 
  The class 'ModelBundle\EntityRest\Level2' was not found in the chain configured namespaces ModelBundle\EntityInternal

我已经尝试了几天所有的事情,这总是我回归的错误......我是否必须将这些实体移到另一个捆绑中?教条是否不可能链接来自不同命名空间或连接的实体?

0 个答案:

没有答案