Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Doctrine DBAL be mixed with ORM Query Builder?

I am trying to create a querybuilder using ORM. But I stumble upon a field on an entity with a relationship to 2 possible tables. With this structure it would be (IMHO) impossible to map it in the entity itself.

╔═══════╗      ╔═══════╗       ╔═══════╗
║ ValB  ║      ║ Main  ║       ║ ValC  ║
╠══╦════╣      ╠══╦════╣       ╠══╦════╣
║ *║ pk ║-- +  ║ *║ pk ║   +---║ *║ pk ║
╠══╬════╣   |  ╠══╬════╣   |   ╠══╬════╣
║  ║    ║   +--║  ║v_id║---+   ║  ║    ║
╠══╬════╣      ╠══╬════╣       ╠══╬════╣
║  ║    ║      ║  ║    ║       ║  ║    ║
╚══╩════╝      ╚══╩════╝       ╚══╩════╝

Is is possible to mix DBAL QueryBuilder with ORM QueryBuilder, or any other way that will still use the ORM QueryBuilder mostly on the code.

PS. I did not design the db and im just optimizing it. sorry for this :(

like image 927
rrw Avatar asked Jun 10 '16 07:06

rrw


1 Answers

No it doesn't make sense, because later ORM have to map results to objects. You can't map both ValB and ValC to the same property in Main

To do it correctly, there should be separate fields in Main table for ValB and ValC relations. Even if they have the same value. Like this

╔═══════╗      ╔════════╗       ╔═══════╗
║ ValB  ║      ║ Main   ║       ║ ValC  ║
╠══╦════╣      ╠══╦═════╣       ╠══╦════╣
║ *║ pk ║-- +  ║ *║ pk  ║   +---║ *║ pk ║
╠══╬════╣   |  ╠══╬═════╣   |   ╠══╬════╣
║  ║    ║   +--║  ║vB_id║   |   ║  ║    ║
╠══╬════╣      ╠══╬═════╣   |   ╠══╬════╣
║  ║    ║      ║  ║vC_id║---+   ║  ║    ║
╚══╩════╝      ╚══╩═════╝       ╚══╩════╝

You could copy v_id column and then make proper mapping in Doctrine.

like image 113
Jakub Matczak Avatar answered Sep 22 '22 07:09

Jakub Matczak