Compound key relationship with fuelphp orm

120 views Asked by At

I am trying to get FuelPHP's orm to use a compound key when using the related() method.

I have a db setup with two tables and I'd like to relate them based on their compound keys.

For example I would normally use a relation like this in my orm model:

protected static $_has_one = [
    'message_flag' => [
        'key_from' => 'object_id',
        'model_to' => '\ModelClass',
        'key_to' => 'object_id',
        'cascade_save' => false,
        'cascade_delete' => false,
    ],
];

But instead of using key_from => 'id and key_to => 'object_id, I'd like to join them on their composite key, which might look something like this:

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,
    ],
];

To clarify, the top example, which is the one recommended in FuelPHP's docs, creates a query that looks like this:

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`);

But the query that I'd like to build with the ORM would look like this:

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`);
1

There are 1 answers

2
johncorser On BEST ANSWER

Actually, I just tried my hopeful, but undocumented example above

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,
    ],
];

Surprised to say this actually worked. Still, if anyone else has a similar problem, I hope they find this answer.