我必须使用无法修改的现有数据库。 我需要用 hasMany 关系链接两个表 (SGRPART hasMany SSGART)。
困难在于这些表由两列连接且没有一个是主键:
表SGRPART的结构:
IDSGRAPART (int 9)
CGRPART(整数 9)
CSOUSGRP(整数 9)
DESGRPART (varchar 100)
表SSGART的结构:
IDSSGART(整数 9)
CGRPART(整数 9)
CSOUSGRP(整数 9)
DESGRPART (varchar 100)
关系应该是: SGRPART.CGRPART = SSGART.CGRPART 和 SGRPART.CSOUSGRP = SSGART.CSOUSGRP
我在 SGRPART 模型中试过这个,但它没有从表 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')
)
);
}
知道它在 Cakephp 中是否可行吗?如果可行,该怎么做?
谢谢, 詹姆斯
最佳答案
你能试试这个吗
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' => ''
),
);
放入“SGRPART”模型
关于mysql - Cakephp hasMany 有多个不是主键的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734228/