在Symfony中将抽象类和接口与ORM一起使用,找不到接口



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

Src

  • AppBundle

    • 实体
      • 业务单元盒子
      • DriverMax
      • 车载福克斯
    • 型号
      • BusinessUnitInterface
      • UDODriver接口
      • 车辆接口
  • FoxBundle

    • 实体
      • 业务部门
      • 车辆
  • MaxBundle
    • 实体
      • DriverMax

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

<?php
// src/AppBundle/Model/BusinessUnitInterface.php
namespace AppBundleModel;
interface BusinessUnitInterface
{
/**
* @return string
*/
public function __toString();
}

抽象类

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use FoxBundleEntityBusinessUnit as BaseBU;
use AppBundleModelBusinessUnitInterface;
/**
* @ORMEntity
* @ORMTable(name="business_unit_fox")
*/
class BusinessUnitFox extends BaseBU implements BusinessUnitInterface
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* {@inheritDoc}
* @see AppBundleModelBusinessUnitInterface::__toString()
*/
public function __toString() {
return $this->getId();
}
}

配置yml

# Doctrine Configuration
doctrine:
dbal:
default_connection: maxdb
connections:
maxdb:
# ...
foxdb:
# ...
max2db:
# ...
orm:
resolve_target_entities:
AppBundleModelVehicleInterface: AppBundleEntityVehicleFox
AppBundleModelUDODriver: AppBundleEntityDriverMax
AppBundleModelBusinessUnit: AppBundleEntityBusinessUnitFox
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,得到了以下错误

[DoctrineCommonPersistenceMappingMappingException] Class 'AppBundleModelBusinessUnitInterface' does not exist

我缺了什么吗?

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

您忘记了UDODriverInterface和BusinessUnitInterface的Interface后缀。您可以按照Symfony文档在抽象类和接口上进行映射

resolve_target_entities:
AppBundleModelVehicleInterface: AppBundleEntityVehicleFox
AppBundleModelUDODriverInterface: AppBundleEntityDriverMax
AppBundleModelBusinessUnitInterface: AppBundleEntityBusinessUnitFox

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

最新更新