Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-04-04 14:07:06


----- Mensaje original -----
De: Christian Henning <chhenning_at_[hidden]>
Fecha: Miércoles, Abril 4, 2007 7:09 pm
Asunto: [Boost-users] [MultiIndex] Resolving foreign keys
Para: boost-users_at_[hidden]

> Hi there, beware I'm a new to Multiindex and haven't digested the
> whole capabilities of the library, yet.
>
> In my example I have two entities that are connected via a foreign
> key. In this case the key is type of an int. When retrieving the
> objects from a database I end up having two containers containing
> those objects. In the code below there are a customer_row and a
> order_row class. There is a one-to_many relationship between them.
One
> customer can have several order but one order is can only one
> customer. So, my goal is to find an way to create order objects that
> are pointing to the correct row objects.
>
> So far, I don't really see how the MultiIndex lib can be of help for
> me. I have looked at the example six which I think came pretty close
> to my goal. So, taken this example, is it possible to use an key type
> instead of the car_manufacturer pointer?

Your design looks basically correct, but you can have a tighter
relationship between order_rows and their associated customer_rows
as follows:

  struct order_row
  {
    int _id;
    customer_table::iterator _customer_it;
    string _article;

    int customer_id() const { return _customer_it->_id; }

    order_row(
      int id,
      customer_table::iterator customer_it,
      const string& article )
    : _id( id ), _customer_it( customer_it ), _article( article ) {}

    friend ostream& operator<<( ostream& os, const order_row& o )
    {
      os << o._id << " " << o.customer_id() << " " << o._article <<
endl;
      return os;
    }
  };

  ...

  typedef multi_index_container<
     order_row,
     indexed_by<
        ordered_unique< tag< primary >,
           BOOST_MULTI_INDEX_MEMBER(order_row,int,_id)
>,
        ordered_non_unique< tag< foreign >,
           BOOST_MULTI_INDEX_CONST_MEM_FUN(order_row,int,customer_id)
>
>
> order_table;

  ...

  order_table ot;
  ot.insert( order_row( 0, ct.find(97), "Book" ));
  ot.insert( order_row( 1, ct.find(97), "Car" ));
  ot.insert( order_row( 2, ct.find(97), "Dish Washer" ));

Having things arranged this way allows to go from a given
order_row to the associated customer info faster (constant
time) than in your original design (logarithmic lookup on
_customer_id). Depending on your usage scenarios this can
be worthwile.

[...]
> Is there a more advanced way using the MultiIndex lib of
> combining two related objects so I can created my orders
> array? Or in different words can I structure my code more
> efficiently?

With the design I propose, looks to me like there is no
compelling need to define an order object and populate the
orders vector: you can work directly with order_table just
as easily, and as efficiently. Does this sound good to you,
or is there some additional aspect I'm missing?

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net