【问题标题】:cakephp one to many custom relation joinscakephp 一对多自定义关系连接
【发布时间】:2010-12-04 04:08:47
【问题描述】:

我有一个包含许多 IPN 的 Order 表。但是,我没有使用 cakephp 约定,因为 IPN 表来自 Paypal。我想将订单表的 order_num 字段加入 IPN 表的自定义字段。所以它会像: select * from orders left join ipn on orders.order_num = ipn.custom

如何在models/order.php中正确设置模型关系。

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    假设我正确理解了这种关系,我相信这应该可以解决问题。

    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',
            ),
        );
    }
    

    【讨论】:

    • 我试过这个并且它有效,但是我有两个关系 - Order hasMany Orderproduct,Order hasMany IPN。在 Orderproduct 关系中,它是 Orderproduct.order_id = Order.id。设置 var $primaryKey 会使 Orderproduct 停止工作,尽管 IPN 可以工作。
    • 或许你可以在 Ipn 的 beforeFind() 中设置 $primaryKey 的值呢?例如:函数 beforeFind() { $this->Order->primaryKey = 'order_num'; } function afterFind() { $this->Order->primaryKey = 'id'; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多