Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
HasMany - Unset related objects
  • Hi there

    I'm wondering something... If i have an Orm object with has many and do something like this : 

    $mySuperObject->myHasManyRelations = null;

    $mySuperObject->myHasManyRelation = // set new relations here

    The previous objects, no longer used, still exists in database, just with the foreign key set to null.

    I want them deleted instead... i took a look in hasmany.php line 224, with the comment "// if any original ids are left over in the array, they're no longer related - break them"

    Are there some way i don't see to simply delete old related ?

    Thanks
  • You need to get them first, otherwise the ORM doesn't know they exist, and can't determine the delta (what to delete and what was newly added).
  • What do you mean by getting them ? 

    If you're talking about related method in query, it does, like this : $mySuperObject = Model_MySuperObject::query->related('myHasManyRelations') // end of query

    Edit : The Orm know witch are new, because it set the foreign key to null only for the olds relation. What i want is delete them instead of just break relation
  • That doesn't execute anything, unless you add a get() to it.

    I meant the ORM is only aware of relations loaded from the database, it doesn't know what's in the database if you haven't queried it.

    There is a difference between deleting the relation, and deleting the related record(s). When you unset a relation, you delete the relation, not the related record. It just disconnect it, as you have observed.

    If you want to delete them, either call delete() on the related object, or define the relation as 'cascade_delete' => true if you always want to delete instead of disconnect.
  • Well, sorry, i should have been more clear : It's the Grouppermission and Rolepermission from the auth package. When changing the permission, the old data can be deleted from those table, but i will do it manually i guess. 

    At this time, it's just let your sql table with a lot of completly useless record with foreign key set to null. An observer for those classes, deleting olds permissions, would be great. It's planned ?
  • HarroHarro
    Accepted Answer
    Ah, ok.

    There is a many to many relation between Role and Permission, and between Group and Permission, which uses Grouppermission and Rolepermission as join table.

    So when you disconnect a permission, you have to use role->permission or group->permission. It will delete the join table record if you do (which is a feature of the many-many). You only need to access the join table relation (or record) directly if you want to set a custom action selection on a specific relation.
  • Yeah, got it. Thanks.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion