Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Cascade Delete In Two Directions
  • Hello,

    I have two models (amongst others_:

    Item and Offer.

    Relations:
    - An item has many offers
    - Therefore, an offer belongs to an item
    - An offer also has one different item (offerereditem)
    - An item therefore also belongs to offer (offereditem).

    I want to delete an item, as an item is deleted I would like all offers associated with that item to be deleted (in both directions). I would like all the 'has-many' offers to be deleted, which is working fine. I run into an issue when trying to delete the offer that the item belongs to: Primary key cannot be changed.

    How do I delete upwards? Or is my data structure completely wrong? Sorry I'm new to relational data. 

    Thanks in advance.

  • Apologies, I wasn't getting any feedback my post had gone through so have posted the same question multiple times by mistake. Can't see how to delete duplicates!
  • Been noticing odd behaviour with the ajax calls fo the forum. Looking into it now...
  • HarroHarro
    Accepted Answer
    That error is usually an indication of either the wrong relation type (for example you swapped has_one and belongs_to) or you have defined the keys wrong. Or you have defined a PK as FK somewhere, which is not allowed.


  • Thanks again Harro, I'll have a play when I get back to it and post my results.
  • I think it may be the primary key error since the item id is linked as the foreign key in the offer (offereditem). I'll let you know how it goes. Seems a bit strange having to create a new model property that is identical to the primary key just to link the other object though? I do have a relatively obscure data structure I admit.

    Also, just to clarify - doesn't a has_one (or has_many) not have to have a belongs_to in the other model to match?


  • It was indeed the primary key being used as the key_from (used as a foreign key in the other model).

    For the benefit of others -> I managed to get around this problem by creating an observer to set a new property that was identical to the id of the model on insertion to the database. I then keyed from this new property instead of the primary key. This is perhaps a little unclean and sub-optimal but I can't think of a better way to do it... Please let me know if anyone can think of one.

    Anyway, thanks Harro. The AJAX issue on this forum seems to be fine now too.

Howdy, Stranger!

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

In this Discussion