dst67283 2014-02-08 21:51
浏览 49
已采纳

使用join codeigniter活动记录更新表?

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
ekogear\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.

  • 写回答

1条回答 默认 最新

  • dongzhouzhang8696 2014-02-09 04:30
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?