Welcome Guest, Not a member yet? Register   Sign In
How to correctly update batch according to post.id
#5

There are multiple ways to do this, here's one way:
PHP Code:
$postID 1;

$existingTerms = [123]; // you get this by SELECT term_id FROM ci_terms_relationship WHERE post_id = $postID
$updatedTerms = [1345]; // from $this->input->post('categories[]');

$toDelete array_diff($existingTerms$updatedTerms); // filter out all term IDs that exist in DB but are not selected for update
print_r($toDelete); // [2]

$toInsert array_diff($updatedTerms$existingTerms); // filter out all terms not yet stored in DB, but are selected for update
print_r($toInsert ); // [4, 5]

if ($toDelete) {
    
$this->db
        
->where('post_id'$postID)
        ->
where_in('term_id'$toDelete)
        ->
limit(count($toDelete))
        ->
delete('ci_terms_relationship');
}

if (
$toInsert) {
    
$dbData = [];
    foreach (
$toInsert as $termID) {
        
$dbData [] = [
            
'post_id' => $postID,
            
'term_id' => $termID,
            
'type' => 'category'
        
];
    }
    
$this->db->insert_batch('ci_terms_relationship'$dbData);

Reply


Messages In This Thread
RE: How to correctly update batch according to post.id - by Pertti - 06-25-2018, 03:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB