我正在尝试使用related()
方法让FuelPHP的orm使用复合键。
我有一个带有两个表的数据库设置,我想根据它们的复合键来关联它们。
例如,我通常会在我的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
和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`);
答案 0 :(得分:0)
实际上,我只是尝试了上面有希望但未记载的例子
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,
],
];
很惊讶地说这实际上有效。不过,如果其他人有类似问题,我希望他们能找到答案。