Active Records Relations

Hi,

Im hoping anybody have encountered similar problems and come up with a solution.

I am using a myisam database and here are my 2 tables:

table 1: orders

columns: id, customername, time, amount

table 2: order_details

columns: id, order_id, product, quantity

the link between the 2 columns are:

orders.id and order_details.order_id

this is my code on Orders Model

function relation() {

    return array(


           'rel_order_details' => array(self::BELONGS_TO,'OrderDetailsActiveDomain','id'),


           


);

}

this is my code on OrderDetails model:

function relation() {

    return array(


           'rel_orders' => array(self::BELONGS_TO,'OrdersActiveDomain','order_id'),


           


	);

}

==

however, the sql code being generated is:

… select * from orders o left outer join order_details od on (o.id=od.id) where …

but what i actually expected is to generate this sql:

… select * from orders o left outer join order_details od on (o.id=od.order_id) where …

====

any ideas anyone?

your relation is wrong, the relation of Order and Orderdetails is HAS_MANY.




function relation() {

return array(

'rel_order_details' => array(self::HAS_MANY,'OrderDetail','orderid'),


);

}


function relation() {

return array(

'rel_orders' => array(self::HAS_ONE,'Orders','order_id'),


);

}



Thanks for your reply Lenye.

But what complicates my database is that the fields that links the 2 tables do not have the same names

orders.id = order_details.order_id

when i tried your suggestion using the correct fields i get this error:

"Trying to get property of non-object"

Arnold

Try




//In Order model

function relation() {

return array(

'rel_order_details' => array(self::HAS_ONE,'OrderDetail','orderid'),


);

}


//In OrderDetail model

function relation() {

return array(

'rel_orders' => array(self::BELONGS_TO,'Orders','order_id'),


);

}