Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Relationship Issue
  • I'm having some issues with ORM relationships being automatically inserted. To give an example, I have a user table (primary key: id), a site table (primary key: id), and a r_user_site relationship table (user_id, site_id) that stores the relationships. Users can have many sites, and sites can have many users. I setup the many_many properties in each model, and everything seems to work just fine. If I need to assign a user to a site, I fetch the user object and assign it to the users relationship array for the site and then save it. For example: $site->users[] = $user; followed by $site->save();.

    The above code works perfectly fine. The issue arises if I need to change properties on the site object and then resave it after it has already been saved. When I try to resave it, FuelPHP tries to re-insert the user_id and site_id back into the r_user_site relationship table instead of updating it which throws a "duplicate entry" error.

    I've tried running a query to re-fetch the site object, but it still gives me the same error. The only way I can get past this is to redirect, re-fetch the site and then resave it. I cannot resave within the same block of code for the life of me. FuelPHP keeps tossing the duplicate entry error for the relationship table no matter what I do, unless I redriect back and then do it.

    I need to get this to work as I have some code inside an observer for the site model that runs after the site is saved. But whenever I resave the site object, the duplicate error comes up. I'm a bit at a loss here. Any help is greatly appreciated.
  • Which version of Fuel are you on?
  • Version 1.8.0.
  • Can you switch the fuel/packages/orm code to 1.9/dev (from if you don't use composer) and see if you still have the issue?
  • Using the dev version isn't an option for us at the moment. However, I'll see if I can setup 1.9 in a test environment and give it a test. It's a really weird issue.
  • HarroHarro
    Accepted Answer
    Just rename your packages/orm folder, then do a git clone of in the packages directory, or download the zip from and unpack it in the packages/orm directory.

    Once you're finished testing, remove the orm directory, and rename the old one back.

    If it addresses your problem, I can see if I can speed up the 1.8.1 release.

Howdy, Stranger!

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

In this Discussion