Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
has_many models delete/update
  • I have a model that has_many other models that i need to remove selectively/update let's use the Model_User has many Model_Comment example.
    Say that I have a page where user page that can select multiple comments or update multiple comments.
    I can think of 2 ways:
    1. delete all the comments by calling $model_user->comments = null and then save it and re add them (easiest way)
    2. loop through and update/delete selectively , which is faster I am opting for the second one, I manage to do the update by looping manually and comparing the content see if it's updated (is there a better way?)
    but I can't selectively delete certain comments out i tried unsetting it based on the index and it didnt work
  • Never existed, this is how: where('field', 'IN', array(1,3))
  • shohyuken Setiawan wrote on Tuesday 2nd of August 2011:
    I have a model that has_many other models that i need to remove selectively/update let's use the Model_User has many Model_Comment example.
    Say that I have a page where user page that can select multiple comments or update multiple comments.
    I can think of 2 ways:
    1. delete all the comments by calling $model_user->comments = null and then save it and re add them (easiest way)
    2. loop through and update/delete selectively , which is faster I am opting for the second one, I manage to do the update by looping manually and comparing the content see if it's updated (is there a better way?)
    but I can't selectively delete certain comments out i tried unsetting it based on the index and it didnt work

    Could try using where_in() so you can add a list of IDs of the comments once they are selected and it will retrieve only them, and you can delete the comments object.
  • deleting the object individually would result in multiple delete calls to the database I assume?
    How do you call where_in using orm? I grab all the requirements using related('comments')
  • $comment_ids = array();
    
    foreach ($comments as $comment)
    {
        array_push($comment_ids, $comment->id);
    }
    
    $num = \DB::delete('comments')->where_in('id', $comment_ids)->execute();
    
    

    I think that's how it was done but it seems to have disappeared from the docs and core... I'm puzzled now.

Howdy, Stranger!

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

In this Discussion