Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
deleting relations
  • I'm loving the ORM package but am hitting the wall trying to remove a related model. The relations are: parent ->(has_many)->share->(has_one)->detail I'd like to delete a specific share, but keep hitting this: "Primary key cannot be changed" Latest try is this (thinking that perhaps it had something to do with the 'related' linkage):
    unset($share->detail);
    unset($parent->shares[$sharekey]);
    $share->delete(); What am I missing? (Using a different method to delete the 'share' I get a has_one.php error (Call to a member function frozen() on a non-object)
  • You can delete relations in all directions, the Orm will handle orphaning all the children.
    One warning is that you can't use a primary key as a foreign key as well, a primary key cannot be changed once it's been set (for data integrity ensurance) and you need to change a foreign key from time to time. Here's a couple of examples for deleting relations:
    Model_Article belongs_to Model_User
    Model_User has_one Model_Profile
    Model_User has_many Model_Article
    Model_User many_many Model_Group
    // remove belongs_to relation
    $article = Model_Article::query()->related('user')->where('id', 1)->get_one();
    $article->user = null;
    $article->save();
    // alternative method for belongs_to, change the foreign key (don't use both!)
    $article = Model_Article::query()->related('user')->where('id', 1)->get_one();
    $article->user_id = null;
    $article->save();
    
    // remove has_one relation
    $user = Model_User::query()->related('profile')->where('id', 5)->get_one();
    $user->profile = null;
    $user->save();
    
    // remove has_many relation with article id 1
    $user = Model_User::query()->related('articles')->where('id', 5)->get_one();
    unset($user->articles[1]);
    $user->save();
    
    // remove many_many relation with group id 3
    $user = Model_User::query()->related('groups')->where('id', 5)->get_one();
    unset($user->groups[3]);
    $user->save();
    
    // remove all relations for has_many or many_many
    $user = Model_User::query()->related('groups')->where('id', 5)->get_one();
    $user->groups = array();
    $user->save();
    
  • Thx Jelmer, The answer to your question is yes. I have an outline of my problem here: http://fuelphp.com/forums/topics/view/1661/20#5830 (Message Formatting on this forum does not work in Chrome 15)
  • Looks like a bug. Can you create an issue for this at http://github.com/fuel/orm/issues?
  • I got the same message removing a member with cascade_delete = true. (Using 1.1/develop) Model_Member has_one Model_Member_Detail
    Model_Member_Detail belongs_to Model_Member members
    -- id (auto, pk)
    -- hash (unique)
    -- etc... member_details
    -- id (auto, pk)
    -- member_id (unique, fk to members)
    -- etc... if ($member = Model_Member::find_by_hash($hash)) $member->delete(); This code does delete both rows in db but still gives an error "Primary key cannot be changed.". Questions: * is there an other way of deleting with cascade_delete = true without getting the error?
    * do I only set cascade_delete = true on has_one or also at belongs_to? (in mind the answer of WanWizard)
  • *updated think I figured it out - re-reading the manual entry for the millionth time I noticed that 'has-one' should only define a one-to-one rel - I was using it to define (essentially) a many-to-one. Thus, using 'belongs-to' (while feeling a little anti-intuitive) meant that Fuel allows the foreign key to be removed as it is possibly not the 'only' link. At least thats what I figure. Not perhaps the behavior I expected but got it working nonetheless. Hope this helps someone else avoid the same fate.
  • is there an other way of deleting with cascade_delete = true without getting the error?
    You shouldn't get the error, nothing here should touch either teh members.id or member_details.id. This is either a bug or faulty configuration.
    do I only set cascade_delete = true on has_one or also at belongs_to?
    For has-many/belongs-to the answer is always no, deleting one of the many objects belonging to the parent object should never delete the parent because that would also trigger deletion of all the other childeren - something that is probably never your intention. In has-one/belongs-to things are less clear, this is up to you. Should deleting the Model_Member_Detail instance also delete the Model_Member instance to which is belongs?
  • As I see it, 'has_one' and 'has_many' define a downstream relationships, 'belongs_to' defines an upstream relationship.
    Like "Parent has_one Child", and "Child belongs_to Parent". You can delete down (parent->child->delete) but you can't delete up (child->parent->delete) as that would make the children orphans. So yes, it is important to understand the distinction between has_one and belongs_to, although from a database perspective they are the same thing...

Howdy, Stranger!

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

In this Discussion