SubSonic Forums
All Your Database Are Belong To Us

Setup Relationships Between ActiveRecord classes

Latest post 11-07-2008 7:42 PM by bbqchickenrobot. 2 replies.
  • 11-06-2008 3:46 PM

    Setup Relationships Between ActiveRecord classes


    Wondering if it possible to programmatically setup a relation between two activerecord classes?  I don't have a foreignkey setup via the database (dba won't let it happne for whatever reason) so now, via code, I'd like to have, for example, a Orders class that has a collection of type Order class which then has a related Product class that can be linked via a collection in the Order class.  So, Orders would have a OrderCollection of type Order and Order would have a ProductsCollection of type Product and can then be loaded based on primary/foreign key columns (that I specify)?  I tried googling for it and found some broken links to a manymany class or something of the sort... 

    So Orders contains Order which contains Product

    Orders->Order->Products

    using pseudo code i could do something like: 

    Order order = new Order();
    order.UserID = 1;

    Product product = new Product().LoadByKey(240386);
    order.Products.Add(product);

    Orders orders = new Orders();
    orders.OrderCollection.Add( order );
    orders.save();

    Possible? 

  • 11-07-2008 1:39 PM In reply to

    Re: Setup Relationships Between ActiveRecord classes

    Just create a new code file and make it a Partial Class of whatever class you want to add the reference to.

    Then add a Property to that partial class.  Implement the property so it refers to another SubSonic object.  If you have any relations on other objects you can use them as a template.

     

    Here is an example from one of my projects in which no relationship exists because the vehicle table is in another database.

        Partial Class Incident

            <XmlIgnore()> _
          Public Property Vehicle() As Vehicle
                Get

                    If Me.VehicleFK.HasValue Then
                            v = New Vehicle
                            v.LoadAndCloseReader(Vehicle.FetchByParameter("VehicleID", Me.VehicleFK))

                   Else

                           Return Nothing
                    End If


                End Get
                Set(ByVal value As Vehicle)
                    SetColumnValue("VesselReportingFK", value.VesselID)
                End Set
            End Property
        End Class

     

  • 11-07-2008 7:42 PM In reply to

    Re: Setup Relationships Between ActiveRecord classes

    Thanks for the reply.  It just so happened that I stumbled upon a random hidden google entry that talked about this.  THen my browser crashed on me!  Luckily I read enough to implement exactly what you posted. 

    I created a property that will lazyload the related child records once access as a partial class to the SubSonic generated class.  Never even crossed my mind - great platform and it gets better as I learn more!

Page 1 of 1 (3 items) | RSS