Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Breaking relation with belongs_to
  • Hello!

    In the docs it's written :

    // break the relationship established above
    $comment = Model_Comment::find(6);
    $comment->post = null;
    $comment->save();

    But with my app the foreign key is not updated. With the example above, after save in the DB, the post_id key is still at the old value.

    If I do :

    // break the relationship established above
    $comment = Model_Comment::find(6);
    $post = $comment->post;
    $comment->post = null;
    $comment->save();

    It works.

    I don't know if it's a wanted behaviour, so I post here before opening an issue on github.

    I'm using Fuel 1.5.2

  • ORM can only break relations if it knows they exist. Your find() doesn't fetch the relation, so there is nothing to delete.

    In your second example, you trigger lazy loading of the related object, after which the ORM knows the relation, and can delete it.

    Bottom line: this is by design. If you need related data, include the relation on the find, it's much faster then having to query the database multiple times.

Howdy, Stranger!

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

In this Discussion