Cakephp hasMany with multiple foreign keys that are not primary keys
I need to use an existing database that cannot be modified. There are two tables that I need to associate with a hasMany relationship (SGRPART hasMany SSGART).
The difficulty is that these tables are joined with two columns and none of them are primary keys:
SGRPART table structure:
IDSGRAPART (int 9)
CGRPART (int 9)
CSOUSGRP (int 9)
DESGRPART (varchar 100)
SSGART table structure:
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 didn't return related records from the SSGART table:
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 this is possible in CakePHP, and if so, how to do it?
Thanks James
source to share
Could you 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
source to share