CakePHP关联:处理空ID

问题描述 投票:0回答:1

我有这个模特:

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']]
    ]
];

使用此模型,我获得了have itemProforma设置的所有item_delivery_note_id。相反,即使item_delivery_note_idnull,我也很想获得所有这些信息。

我不确定belongsTo是否正确(我的意思是ItemProformas定义)。但是hasOne表示它即使它们没有任何itemProformas关联,检索all ItemDeliveryNote的正确语法是什么?但是如果有,我也需要检索ItemDeliveryNote对象。

我有这个模型:Proforma-> hasMany('ItemProformas',['foreignKey'=>'proforma_id'])); -> belongsTo('Customers',['foreignKey'=>'customer_id']); -> belongsTo('ProformaStates,[...

php sql cakephp associations
1个回答
0
投票
是否必须存在相关记录也主要取决于架构,而不取决于关联的类型。如果外键可为空,则相关记录为可选。是否以及如何实施在应用程序级别上的约束是另一回事。
© www.soinside.com 2019 - 2024. All rights reserved.