0

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.

Dennis
  • 7,907
  • 11
  • 65
  • 115

1 Answers1

1

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.

Jacobson
  • 674
  • 5
  • 10