tags:

views:

282

answers:

1

I have an Order table which has many IPN. However, I am not using cakephp conventions because the IPN table is from Paypal. I want to join the order table's order_num field to the IPN table's custom field. So it would be like: select * from orders left join ipn on orders.order_num = ipn.custom

How do I set up the model relation correctly in models/order.php.

A: 

I believe this should do the trick, assuming I'm correctly understanding the relationship.

class Order extends AppModel {
    var $primaryKey = 'order_num';

    var $hasMany = array(
        'Ipn' => array(
            'className'  => 'Ipn',
            'foreignKey' => 'custom',
        ),
    );
}

class Ipn extends AppModel {
    var $belongsTo = array(
        'Order' => array(
            'className'  => 'Order',
            'foreignKey' => 'custom',
        ),
    );
}
Matt Huggins
I tried this and it works but, I have two relations - Order hasMany Orderproduct, Order hasMany IPN. In the Orderproduct relation it is Orderproduct.order_id = Order.id. Setting the var $primaryKey makes Orderproduct stop working, although IPN works.
jimiyash
Perhaps you could set the $primaryKey value in the beforeFind() of Ipn then? e.g.: function beforeFind() { $this->Order->primaryKey = 'order_num'; } function afterFind() { $this->Order->primaryKey = 'id'; }
Matt Huggins