Nanne Huiges Wed Jun 08 07:56:10 -0400 2011

Subject: has_many association problem

I'm having a small problem with association. I have different names then phpactiverecord uses, and I'm trying to define a has_many assosiation between a shop and an order. (i've translated the names for clarity.

A shop has multiple orders connected to it, and the order -> shop association is defined by using a belongs_to. This works, forinstance, I can do this

$order = Order::find(1);         //find order with id 1
$shop = $order->shop; //find the shop to prove association

Now to do this "the other way around", I thought to define a has_many association in shop, so that I could do this

$shop    = Shop::find(1);       //find shop with id 1
$orders = $shop->orders; //find orders that belong to this shop

But that does not seem to work. The associations are like this:
Tables:
Shop has a shopId and some other info.
Order has an orderId, shopId and some info)

in Order class (this seems to work, as I can request the shop from the order)

static $belongs_to = array(
array('shop','foreign_key'=>'shopId', 'primary_key'=>'shopId',
'class_name'=>'Shop'),
);

in Shop class (I can not get orders from the shop like this)

static $has_many = array(
array('order','foreign_key'=>'shopId', 'primary_key'=>'shopId',
'class_name'=>'Order')
);

Maybe i'm doing something wrong with the way that I request the orders from a shop, or maybe i've made a mistake in the has_many association? Anyway, I'm a bit stuck :(


Nanne Huiges Fri Jun 10 02:43:59 -0400 2011

It looks like the connection works when I use the lower-case key-names:

static $has_many = array(
array('order','foreign_key'=>'shopid', 'primary_key'=>'shopid',
'class_name'=>'Order')
);

although the belongs_to is defined (and working!) with the camelCases:

static $belongs_to = array(
array('shop','foreign_key'=>'shopId', 'primary_key'=>'shopId',
'class_name'=>'Shop'),
);

Is there some sort of rule on how to deal with camelCasing in fieldnames?

Zach Brown Fri Jun 10 12:08:51 -0400 2011

I've noticed that phpactiverecord makes all your field names lower case - even if you're using an alias. Try accessing the object properties with lowercase.

(1-2/2)