1
public function addToTransfer($data)
    {

        $countProductsAdded = $this->where('product_id', $data['product_id'])
                ->where('product_transfer_id', $data['product_transfer_id'])
                ->where('transfer_id', $data['transfer_id'])
                ->countAllResults();
        
        for($i = $countProductsAdded; $i < $data['quantity']; $i++) {
            

            $dataToUpdate = [
                'transfer_id' => $data['transfer_id'],
                'product_transfer_id' => $data['product_transfer_id'],
                'status' => $data['status'],
                'warehouse' => $data['new_warehouse']
            ];
            
            $response = $this->set($dataToUpdate)
                ->where('product_id', $data['product_id'])
                ->where('status', 'instock')
                ->where('warehouse', $data['old_warehouse'])
                ->update();

        }
        return $response;
    }

Hello, i have a problem with this loop, i don't understand what could be wrong here is a black box for now and i don't understand what's wrong.

I will explain what i checked.

  1. $countProductsAdded - returned correctly (it's counting already added results from database)
  2. $data['quantity'] - returned correctly (how many times i want to run that loop)

What i expect, i call this loop with quantity = 5, i expect to run 5 times and update 5 rows from database which meets some criterias.

what i get, totally different :)), is update all rows from database which meets those criterias.

It's not understandable for me because same loop, same logic is working for an insert call

BTW, it's php, codeigniter 4

  • 3
    You are overwriting the response in every iteration and you are returning only the last one. – Markus Zeller Aug 30 '23 at 14:24
  • it was added just to see if it's working, this dosen't impact the function, even if i remove that return, get the same result. – Nelu Constantin Aug 30 '23 at 14:45
  • I never used codeigniter, but I assume the where clause is wrong and matches more than one entry. Are you sure you do not overwrite the where clause? I've seen `where(...)->andWhere(...)` in other code bases. – Markus Zeller Aug 30 '23 at 16:12

1 Answers1

1

Ok Guys, i figured out somehow, basically, i stopped using for loop and tried other approach, i will let example here, maybe it will help someone else as a practical example

    public function addToTransfer($data)
{
    $countProductsAdded = $this->where('product_id', $data['product_id'])
        ->where('product_transfer_id', $data['product_transfer_id'])
        ->where('transfer_id', $data['transfer_id'])
        ->countAllResults();

    $rowsToSelect = $data['quantity'] - $countProductsAdded; 
    if ($rowsToSelect > 0) {     

        $query = $this->select('*')
            ->where('product_id', $data['product_id'])
            ->where('status', 'instock')
            ->where('warehouse', $data['old_warehouse'])
            ->limit($rowsToSelect)
            ->get(); // Get the query instance

        $rowsToUpdate = $query->getResultArray();
        
        $updatedRowCount = 0;
        foreach ($rowsToUpdate as $row) {
            $this->set([
                'transfer_id' => $data['transfer_id'],
                'product_transfer_id' => $data['product_transfer_id'],
                'status' => $data['status'],
                'warehouse' => $data['new_warehouse']
            ])
            ->where('id', $row['id'])
            ->update();
        
            $affectedRows = $this->affectedRows();
            if ($affectedRows > 0) {
                $updatedRowCount++;
                $updatedRowIds[] = $row['id'];
            }    
        }
        return ['updated_rows' => $updatedRowCount, 'updated_rows_ids' => $updatedRowIds];
    } 
    else{
        return ['updated_rows' => 0, 'updated_rows_ids' => false];
    }  
}