ikkez / f3-cortex

A multi-engine ORM / ODM for the PHP Fat-Free Framework
GNU General Public License v3.0
118 stars 22 forks source link

many-to-any relation delete #129

Open olaulau opened 7 months ago

olaulau commented 7 months ago

Hi ;

Let's say we have modelA <-> modelB linked by a many-any relation (and a pivot table). I can use $modelA->Bs to get every modelB linked to my modelA.

Now if I try to set this relation, I can write : $modelA->Bs = [1, 2, 3], 1, 2, 3 being IDs of my modelB I want to link. This works.

Viewing the DB logs, it seems that cortex deletes all before inserting them again (one by one). Although it's useless (if I only remove one of the IDs), if we have other data linked, it can be problematic (and fail due to FK constraint).

Using only collections (no array of IDs), we can add with this syntax : $modelA->Bs [] = $b. How about to remove one ? I tried this :

foreach($modelA->Bs as $i => $bs) {
    unset($modelA->Bs [$i]);
}
$modelA->save();

but it didn't work.

Any idea ? I think I will fallback querying directly the pivot table, but it sucks.