5

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 :(

rrw
  • 671
  • 7
  • 18

1 Answers1

5

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.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
  • Yes, I am aware of that solution, but is there a way for this not resorting into creating another table? because we are in production and its very tedious and dangerous to migrate into this structure.. – rrw Jun 10 '16 at 07:29
  • 1
    I understan your problem. But no. You probably will have to stay with DBAL only here. – Jakub Matczak Jun 10 '16 at 07:30
  • thank you ur answer enlightened me. I was just thinking that maybe there is a way to dynamically map the entity. But I think there's no way. Thanks! :) – rrw Jun 10 '16 at 07:32
  • 1
    Wait a second. Maybe you could try with this: http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/native-sql.html#resultsetmappingbuilder I've never tried it, but it looks nice. – Jakub Matczak Jun 10 '16 at 07:35
  • Looks like this is the one I'm looking for :) havent heard of it yet but sure looks like my solution :) thanks!! ill try this one out. Btw, you should add it to your answer.. – rrw Jun 10 '16 at 07:58