In my current Zend Framework 2 project I have a data schema like this:
car
id
type -> ENUM('Tesla', 'AstonMartin', 'Bmw')
common_attribute
car_tesla
car_id
specific_attribute_a
specific_attribute_b
specific_attribute_c
car_aston_martin
car_id
specific_attribute_a
specific_attribute_b
specific_attribute_d
car_bmw
car_id
specific_attribute_b
specific_attribute_d
specific_attribute_e
On the application level it's an inheritance, where Car
is an abstract super-class and CarFoo
, CarBar
, and CarBuz
its sub-classes.
I want to retrieve the entire data and build in the next step concrete Car
objects.
To make the building of objects from the selected data easier, I'm using prefixes:
$sql = new Sql($this->dbAdapter);
$select = $sql->select('car')->columns('car_id' => 'id' ...);
$select->join('car_tesla', 'car_tesla.car_id = car.id', [
'car_tesla_specific_attribute_a' => 'specific_attribute_a',
...
'car_tesla_specific_attribute_c' => 'specific_attribute_c'
]);
$select->join('car_aston_martin', 'car_aston_martin.car_id = car.id', [
'car_aston_martin_specific_attribute_a' => 'specific_attribute_a',
...
'car_aston_martin_specific_attribute_d' => 'specific_attribute_d'
]);
...
It's a lot duplicated code...
How to build the statement with Zend\Db
the way, that every column gets a prefix (e.g. the name of its table) automatically / defined at one place?
Here I'm trying to resolve the problem on the database level.