简体   繁体   中英

update table using join codeigniter active record?

here is a snippet of my code using active record to update the item_stock values. I need to get the item.SKU from item table as selector to my query.

$this->db->join('item','item.item_id = items.item_id')
          ->join('items','items.stock_id = item_stock.stock_id')
          ->set('item_stock.stock_quantity','item_stock.stock_quantity + $new_qty',FALSE)
          ->where('item_stock.colour',$color)
          ->where('item_stock.size',$size)
          ->where('item.SKU',$SKU)
          ->update('item_stock');
$query = $this->db->update('item_stock');

for some reason it lost it's JOIN stack.

Error Number: 1054

Unknown column 'item.SKU' in 'where clause'

UPDATE `item_stock` SET `item_stock`.`stock_quantity` = item_stock.stock_quantity + $new_qty WHERE `item_stock`.`colour` = 'Kuning' AND `item_stock`.`size` = 'XL' AND `item`.`SKU` = 'Wooser-01'

Filename: E:\xampp\htdocs\nekogear\system\database\DB_driver.php

Line Number: 330

any tips how to overcome this problem? thanks before.


* ps = I've try the $this->db->query("query code here") and it works fine, but I want to use the active record style for the consistency.

Call me stupid but hey, it's works! My "solution" is to include the JOIN in the $this->db->update()

$this->db->set('item_stock.stock_quantity','item_stock.stock_quantity + $new_qty',FALSE)
         ->where('item_stock.colour',$color)
         ->where('item_stock.size',$size)
         ->where('item.SKU',$SKU);
$query = $this->db->update('item_stock JOIN items ON items.stock_id = item_stock.stock_id JOIN item ON item.item_id = items.item_id');

Its equal to the following query

UPDATE `item_stock`
JOIN items ON items.stock_id = item_stock.stock_id
JOIN item ON item.item_id = items.item_id
SET `item_stock`.`stock_quantity` = item_stock.stock_quantity + $new_qty
WHERE `item_stock`.`colour` = 'Kuning'
AND `item_stock`.`size` = 'XL'
AND `item`.`SKU` = 'Wooser-01'

Hope it will helps you too!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM