php-activerecord
                                
                                 php-activerecord copied to clipboard
                                
                                    php-activerecord copied to clipboard
                            
                            
                            
                        Weird if in eager loading causing missing records
I'm running tests in my REST app and noticed a strange behavior: using Page::find(1)->definitions (a has-many-through relation, passing by page_has_definitions) relationship gives me two entries, as expected, but Page::find(1, ['include' => ['definitions']]) gives me only one.
After digging a lot the problem, I found the problem is here: https://github.com/kla/php-activerecord/blob/master/lib/Relationship.php#L194
It compares the IDs of the original table and the related one, but... Those are different fields in case of through associations. The comparison should happen between the original table to the through table, and then from the related table and the through table.
Unfortunately I found the code confusing enough for not getting courage to mess with it. Any ideas? :(
Ignore all those comments and commits, it's just me trying to mess with the code and fix the problem.
This vexes me greatly. I'm also trying to build a REST API, and I've hit the same dead-end. Here's my fix in Relationship.php around line 170:
            // Copy over the foreign key from the association
            if( !isset( $options['select'] ) ) {
                $options['select'] = $this->get_table()->get_fully_qualified_table_name(). '.*';
            }
            $options['select'] .= ", " . $through_table->get_fully_qualified_table_name();
            $options['select'] .= ".{$fk[0]} AS {$fk[0]}";
            // Changed from $this->primary_key[0]
            $query_key = $fk[0];
I don't know if this breaks something yet, I'll report back if it does.
[edit] It might be good to save the foreign key as a read-only attribute, since saving the model might not be possible with the extra column added with AS. I'll need to do more testing... [/edit]
Please note that it also doesn't account for duplicate entities, you'll have to do that in your business logic. For example, in my database, I have entries, objects, and images. In one instance, there are three objects associated with one entry. Then, there's two images associated with those objects in total. Each of these objects currently has only one image associated with it (though that may change in the future), but there is one image that displays two of these objects, thus it's associated with both. Therefore, my output looks something like this:
{
    "entries":[
        {
            "id":17,
            "objects":[
                {
                    "id":17,
                    "images":[
                        {
                            "id":116
                        }
                    ]
                },
                {
                    "id":18,
                    "images":[
                        {
                            "id":116
                        }
                    ]
                },
                {
                    "id":19,
                    "images":[
                        {
                            "id":117
                        }
                    ]
                }
            ]
        }
    ]
}