You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
Viewing the DB logs, it seems that cortex deletes all before inserting them again (one by one).
fixed this issue in the latest commit. Hope this helps.
Regarding unset($modelA->Bs [$i]); ... this is problematic, because PHP uses a reference to update the underlaying array. The way it is implemented currently only pushes updates to the field when changing the value via set, so meaning using $modelA->Bs = $foo, because in this case it's not modified indirectly.. blame PHP for such weirdness behaviour :D
I'll certainly need to check all loaded relations and see if they have changed on saving the model to improve the usage at this point.. that's a task for another day ;)
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 :
but it didn't work.
Any idea ?
I think I will fallback querying directly the pivot table, but it sucks.
The text was updated successfully, but these errors were encountered: