Compound key relationship with fuelphp orm

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

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.

链接地址: http://www.djcxy.com/p/64816.html

上一篇: C,我如何测试对象类型?

下一篇: 与fuelphp orm的复合关键关系