Cakephp hasMany with multiple foreign keys that are not primary keys

852 views Asked by At

I have to use an existing database that can't be modified. There are two tables I need to link with a hasMany relationship (SGRPART hasMany SSGART).

The difficulty is that these tables are joined with two columns and none are primary keys:

Structure of table SGRPART:
IDSGRAPART (int 9)
CGRPART (int 9)
CSOUSGRP (int 9)
DESGRPART (varchar 100)

Structure of table SSGART:
IDSSGART (int 9)
CGRPART (int 9)
CSOUSGRP (int 9) DESGRPART (varchar 100)

The relationship should be: SGRPART.CGRPART = SSGART.CGRPART AND SGRPART.CSOUSGRP = SSGART.CSOUSGRP

I tried this in the SGRPART model but it doesn't return the associated records from the table SSGART:

class SGRPART extends AppModel
{
public $name = 'SGRPART';
public $useTable = 'SGRPART';
public $primaryKey = 'IDSGRPART';
public $displayField = 'DESGRPART';

var $hasMany = array(
'SSGART' => array(
'foreignKey' => false,
'conditions' => array('SSGART.CGRPART' => 'SGRPART .CGRPART','SSGART.CSOUSGRP'=>'SGRPART .CSOUSGRP')
)
);

}

Any idea if it's feasible in Cakephp and if yes how to do it?

Thanks, James

1

There are 1 answers

2
Karthik Keyan On

Could You pls try this

public $hasMany = array(
    'SSGART' => array(
        'className' => 'SSGART',
        'foreignKey' => 'IDSGRPART',
        'dependent' => true,
        'conditions' => array('SSGART.CGRPART' => 'SGRPART .CGRPART','SSGART.CSOUSGRP'=>'SGRPART .CSOUSGRP')
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
); 

put it in "SGRPART" model