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 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流