【发布时间】:2020-05-19 07:23:14
【问题描述】:
我有这个模型:
Proforma
->hasMany('ItemProformas', ['foreignKey' => 'proforma_id']);
->belongsTo('Customers', ['foreignKey' => 'customer_id']);
->belongsTo('ProformaStates', ['foreignKey' => 'proforma_state_id']);
->hasMany('Invoices', ['foreignKey' => 'proforma_id']);
ItemProformas
->belongsTo('Proformas', ['foreignKey' => 'proforma_id', 'joinType' => 'INNER']);
->belongsTo('ItemDeliveryNotes', ['foreignKey' => 'item_delivery_note_id']);
ItemDeliveryNotes
->belongsTo('DeliveryNotes', ['foreignKey' => 'delivery_note_id', 'joinType' => 'INNER']);
->belongsTo('ItemOrders', ['foreignKey' => 'item_order_id']);
->belongsTo('ItemOrdersTypes', ['foreignKey' => 'item_orders_type_id']);
->belongsTo('Products', ['foreignKey' => 'product_id']);
每个ItemProforma 可能有一个ItemDeliveryNotes,否则外键将是null。这是我的paginate 电话:
$this->paginate = [
'contain' => [
'Customers',
'ProformaStates',
'ItemProformas' => ['ItemDeliveryNotes' => ['DeliveryNotes']]
]
];
有了这个模型,我得到了 拥有 item_delivery_note_id 集的所有itemProforma。相反,即使item_delivery_note_id 是null,我也很想得到它们。
我不确定belongsTo 在这里是否正确(我的意思是在ItemProformas 定义中)。但是hasOne 暗示它有一个关联的行,而不是可能有一个。
检索 all itemProformas 的正确语法是什么,即使它们没有关联任何 ItemDeliveryNote?但如果他们有,我还需要检索 ItemDeliveryNote 对象。
【问题讨论】:
标签: php sql cakephp associations