multiple belongTo relation in one model for same foreign key in cakephp 3

106 views Asked by At

I have one model SupplierInquiry that has many SupplierInquiryProducts and in SupplierInquiryProducts table I have product_id foreign key and product_id can be from many model. I have applied relation in SupplierInquiryProducts like below

    $this->belongsTo('ConsumablesMaster', [
            'foreignKey' => 'product_id',
            'className' => 'ConsumablesMaster'
    ])->setConditions(['SupplierInquiry.inquiry_type'=>'Consumable']);

    $this->belongsTo('ProductsMaster', [
            'foreignKey' => 'product_id',
            'className' => 'ProductsMaster'
    ])->setConditions(['SupplierInquiry.inquiry_type'=>'Chemical Product']);

  public function buildRules(RulesChecker $rules){
    $rules->add($rules->existsIn(['supplier_inquiry_id'], 'SupplierInquiry'));
    $rules->add($rules->existsIn(['product_id'], 'ConsumablesMaster'));
    $rules->add($rules->existsIn(['product_id'], 'ProductsMaster'));
    return $rules;
  }

Here at a time both belongTo is not working and also for applied condition it's giving error that Column not found: 1054 Unknown column 'SupplierInquiry.inquiry_type' in 'on clause'

1

There are 1 answers

0
Ved On

Column not found: 1054 Unknown column 'SupplierInquiry.inquiry_type' in 'on clause'

Reason for this error is that you should have inquiry_type in your supplier_inquiry table of database which might be missing, Re-bake your models also may this solves your problem.