Cómo vincular un campo de tabla a diferentes tablas (asignación de herencia con la doctrina)

I have a log table which has a special field named "source" that I would like to link to different tables depending on the source.

With native sql, I would do something like create 2 fields: source_id and source_type then customize the query to join to the appropriate tables. I wonder if there is any way to do this with doctrine?


It seems like I can use inheritance mapping for this purpose. I run into another issue, however: lets say that I now have 2 fields: source and destination which maps to different objects. So an object can be either a source or a destination, I'm not sure what to do in this case.

In Doctrine example, it looks like I can create a parent class named Sourceable then have my objects entities inherit it, but now since these entities can be either Sourceable or Destinationable and PHP does not allow multiple inheritance I dont't know what to do.


I found a Ruby case that covers this issue: https://github.com/spree/spree/blob/master/core/app/models/spree/adjustment.rb

class Adjustment < ActiveRecord::Base
    belongs_to :adjustable, polymorphic: true
    belongs_to :source, polymorphic: true
    belongs_to :originator, polymorphic: true

The source is where an adjustment was triggered from. For tax and promotional adjustments, this will be the order itself. For shipping adjustments, this will be the shipment which corresponds with the shipping method for this order.

The adjustable is the object being adjusted, which is the order.

The originator is the object responsible for the adjustment. For promotional adjustments, this will be a Spree::Promotion::Actions::CreateAdjustment object. For tax adjustments, a Spree::TaxRate object. For shipping adjustments, a Spree::ShippingMethod object.

In their case the order can be either a source or an adjustable. Obviously this example is in ruby and with active record, but I wonder how I can do the same thing with Doctrine?

preguntado el 21 de septiembre de 13 a las 07:09

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.