Feature Request #3202

Allow ORM::remove() to delete all relationships

Added by Ivan Brotkin about 4 years ago. Updated almost 4 years ago.

Status:ClosedStart date:08/27/2010
Priority:NormalDue date:
Assignee:Lorenzo Pisani% Done:

0%

Category:-
Target version:Kohana v3.x - v3.1.0
Resolution:fixed Points:

Description

If I want to delete all user roles (HABTM), I need smth like this:

foreach ($user->roles->find_all() as $role)
{
    $user->remove('roles', $role);
} 

So, its a one DB query per role - too many, I think. Thats why I want to drop all roles when I put NULL instead of role model:

// clear user roles
$user->remove('roles');

I created a gist with new feature implemented.


Related issues

Related to ORM - Feature Request #2992: ORM::has() for multiple objects Closed 06/22/2010
Precedes Kohana v3.x - Feature Request #3203: Optional support for deleting relations. Closed 08/28/2010 08/28/2010

History

#1 Updated by Kiall Mac Innes about 4 years ago

  • Target version changed from v3.0.8 to v3.1.0

I haven't read the patch (yet) but.. This would need to be an optional feature (via a model property).. I'd prefer to let my database handle this via standard FKs rather than have Kohana issue a delete query per relation.

But - I do think its a good feature to have..

#2 Updated by Kiall Mac Innes about 4 years ago

Just re-read the sample code and read the gist ..

The code as its written looks like it works and is indeed optional.. I had figured this deletion would be triggered by $model->delete(); which in turn, deletes all relations..

#3 Updated by Ivan Brotkin about 4 years ago

I dont want to use it instead of DB triggers or constraints :)

Also, what about passing multiple objects to add(), has() and remove() methods? For example:

// remove 'admin' and 'login' roles
$user->remove('roles', array(1,2));
// check if user has admin or moderator roles
if ($user->has('roles', array($admin_role_object, $moderator_role_object))) {...}

#4 Updated by Kiall Mac Innes about 4 years ago

Ah - so your wanting to keep the parent record, and just delete the relations? That makes sense too I suppose..

Re multiples - I don't see a reason this can't work.. But if it should be included would be more John Heathco's area than mine ;)

#5 Updated by Kiall Mac Innes about 4 years ago

Issue #2992 is related to the multiples idea.

#6 Updated by Ivan Brotkin about 4 years ago

Ha! Its my ticket :) confused Two months ago...

Well, may be I should send a push request with this features implemented?

#7 Updated by Kiall Mac Innes about 4 years ago

I believe ORM is going to be getting a bit of an internals makeover in 3.1 (I'm not 100% on that though) so it may be better to get John's attention and see what he's planning ;)

#8 Updated by Ivan Brotkin about 4 years ago

Thats why I want to implement this with 3.0.x branch (there is no major API changing). As I know, 3.1 will be released at the end of 2010 - I cant wait so long ;)

#9 Updated by Ivan Brotkin about 4 years ago

PS. John Heathco, last visit - 26.07.2010 :(

#10 Updated by Isaiah DeRose-Wilson about 4 years ago

  • Project changed from Kohana v3.x to ORM
  • Category deleted (Modules:ORM)

#12 Updated by Isaiah DeRose-Wilson almost 4 years ago

  • Status changed from New to Closed
  • Assignee changed from John Heathco to Lorenzo Pisani
  • Resolution set to fixed

Fixed by 0339effb17...

Also available in: Atom PDF