tattersoftware / codeigniter4-relations

Entity relationships for CodeIgniter 4
MIT License
87 stars 20 forks source link

Empty Object Return from related tables #17

Closed eafarooqi closed 3 years ago

eafarooqi commented 3 years ago

Hi,

I have a table meetings which is linked to the table teams with table meetings_teams. The relation works fine but, with and empty object loads the data from the related table.

$meeting = new Meeting();
print_r($meeting->teams);

this should come blank but it loads teams from the teams table.

for now i have added the else to BaseTrait to return empty if there is no primary key or ids defined to load the related data. This need to be fixed properly. i have just added this code to make my project run correctly for now.

// Entities always filter by themselves
if (! empty($this->attributes[$this->primaryKey]))
{
    $builder->where("{$originating}", $this->attributes[$this->primaryKey]);
}
// Check for an explicit filter request
elseif (! empty($ids))
{
    $builder->whereIn("{$originating}", $ids);
} else {
    return [];
}
MGatner commented 3 years ago

Good catch! This should probably go on Entity::relations() though, skip the call to the BaseTrait altogether. Please submit a PR if you have the time.

MGatner commented 3 years ago

Closed by #19