我试图让 FuelPHP 的 orm 在使用该related()
方法时使用复合键。
我有一个带有两个表的数据库设置,我想根据它们的复合键来关联它们。
例如,我通常会在我的 orm 模型中使用这样的关系:
protected static $_has_one = [
'message_flag' => [
'key_from' => 'object_id',
'model_to' => '\ModelClass',
'key_to' => 'object_id',
'cascade_save' => false,
'cascade_delete' => false,
],
];
但是我不想使用key_from => 'id
and key_to => 'object_id
,而是想将它们加入到他们的复合键上,这可能看起来像这样:
protected static $_has_one = [
'message_flag' => [
'key_from' => ['object_id', 'other_key'],
'model_to' => '\ModelClass',
'key_to' => ['object_id', 'other_key',
'cascade_save' => false,
'cascade_delete' => false,
],
];
为了澄清起见,FuelPHP 文档中推荐的最上面的示例创建了一个如下所示的查询:
SELECT `t0`.`object_id` AS `t0_c0`, `t0`.`other_key` AS `t0_c1` FROM `myTable` AS `t0` LEFT JOIN `otherTable` AS `t1` ON (`t0`.`object_id` = `t1`.`object_id`);
但是我想用 ORM 构建的查询看起来像这样:
SELECT `t0`.`object_id` AS `t0_c0`, `t0`.`other_key` AS `t0_c1` FROM `myTable` AS `t0` LEFT JOIN `otherTable` AS `t1` ON (`t0`.`object_id` = `t1`.`object_id` AND `t0`.`other_key` = `t1`.`other_key`);