duanping1920 2015-11-24 23:24
浏览 16

Doctrine是否支持复合实体?

In my legacy code I have this class . . .

class Motor
{
    //db table motor_part_sizes
    //physical dimensions relating to various screw sizes
    private $a;
    private $b;
    private $c;

    //db table motor_part_length
    //also dimensions relating to length
    private $adapter_length;
    private $motor_length;
}

The above object in legacy code is pulled from two tables.

When converting my table entities to Doctrine, I created two entities. Now my problem is that existing legacy code expects the above object to contain data from both entities.

I can certainly craft an raw SQL query that loads up the above object and that is how it is done in the code, but when it comes to Doctrine, how do I go about creating such a composite entity without having to ransack a lot of existing legacy code from the get go?

Is there a different way to solve this altogether? What I did currently was to load up two Doctrine entities separately, and then use a factory method to populate existing object above. Essentially I do

$motor->setDataFromTable1($data1);
$motor->setDataFromTable2($data2);

So I resolved this via OOP. Curious about more Doctrine-centric approaches.

  • 写回答

1条回答 默认 最新

  • drtkyykai004574380 2015-11-25 00:15
    关注

    According to doctrine docs, mapping many tables to one entity is not possible by the doctrine architecture. So, any solution will have to be outside doctrine API boundaries. For further info, take a look at this documentation section: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/limitations-and-known-issues.html

    I think the best solution is changing your Motor class to be a kind of adapter, like this:

    class Motor {
    
        private TableEntityA $tableA;
        private TableEntityB $tableB;
    
        public function setTableEntityA($tableA) {
            $this->tableA = $tableA;
        }
    
        public function setTableEntityB($tableB) {
            $this->tableB = $tableB;
        }
    
        public function getFieldA1() { $this->tableA->getFieldA1(); }
        public function setFieldA1($value) { $this->tableA->setFieldA1($value); }
    
        public function getFieldA2() { $this->tableA->getFieldA2(); }
        public function setFieldA2($value) { $this->tableA->setFieldA2($value); }
    
        public function getFieldB1() { $this->tableB->getFieldB1(); }
        public function setFieldB1($value) { $this->tableB->setFieldB1($value); }
    
        public function getFieldB2() { $this->tableB->getFieldB2(); }
        public function setFieldB2($value) { $this->tableB->setFieldB2($value); }
    
    }
    

    Doing this way you keep your data up-to-date and consistent through entities.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大