在Symfony中使用抽象类和接口与ORM,未找到接口

时间:2017-02-07 07:23:40

标签: php symfony doctrine-orm

我有一个使用三个不同数据库的项目,第一个是主数据库,我可以对后两个数据库进行更改属于不同的项目,但我需要访问其中的数据。以下是简化的目录结构。

Src

  • 的appbundle

    • 实体
      • BusinessUnitsFox
      • 的DriverMax
      • VehicleFox
    • 模型
      • BusinessUnitInterface
      • UDODriverInterface
      • VehicleInterface
  • FoxBundle

    • 实体
      • BusinessUnits
      • 车辆
  • MaxBundle
    • 实体
      • 的DriverMax

以下是BusinessUnits的抽象类和接口的文件,车辆和驱动程序类似。

<?php
// src/AppBundle/Model/BusinessUnitInterface.php

namespace AppBundle\Model;

interface BusinessUnitInterface
{
    /**
     * @return string
     */
    public function __toString();
}

抽象类     

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FoxBundle\Entity\BusinessUnit as BaseBU;
use AppBundle\Model\BusinessUnitInterface;

 /**
  * @ORM\Entity
  * @ORM\Table(name="business_unit_fox")
  */
class BusinessUnitFox extends BaseBU implements BusinessUnitInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
    * {@inheritDoc}
    * @see \AppBundle\Model\BusinessUnitInterface::__toString()
    */
    public function __toString() {
        return $this->getId();
    }
}

config.yml

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: maxdb
        connections:
            maxdb:
                # ...
            foxdb:
                # ...
            max2db:
                # ...
    orm:
        resolve_target_entities:
            AppBundle\Model\VehicleInterface: AppBundle\Entity\VehicleFox
            AppBundle\Model\UDODriver: AppBundle\Entity\DriverMax
            AppBundle\Model\BusinessUnit: AppBundle\Entity\BusinessUnitFox
        auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: maxem
        entity_managers:
            maxem:
                connection: maxdb
                mappings:
                    AppBundle: 
                    BWTCalendarBundle: ~
                    BWTFMBundle: ~
                    BWTHealthCheckBundle: ~
                    BWTSkytrackBundle: ~
                    BWTTelematicsBundle: ~
            foxem:
                connection: foxdb
                mappings:
                    FoxBundle: ~
            max2em:
                connection: max2db
                mappings:
                    MaxBundle: ~

当我进行SQL调用时,我收到以下错误

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'telematics.vehicle_fox' doesn't exist

所以我运行doctrine:schema:update并收到以下错误

[Doctrine\Common\Persistence\Mapping\MappingException] Class 'AppBundle\Model\BusinessUnitInterface' does not exist

我有什么遗失的吗?

是否可以从抽象类声明OneToMany关系?

1 个答案:

答案 0 :(得分:0)

您忘记了UDODriverInterface和BusinessUnitInterface的接口后缀。您可以按照Symfony doc在Abstract classes和Interfaces上进行映射

resolve_target_entities:
            AppBundle\Model\VehicleInterface: AppBundle\Entity\VehicleFox
            AppBundle\Model\UDODriverInterface: AppBundle\Entity\DriverMax
            AppBundle\Model\BusinessUnitInterface: AppBundle\Entity\BusinessUnitFox

另请注意当前声明,您的AppBundle \ Entity \ BusinessUnitFox类不是抽象的。