1

I am using zend, I have the following code like

class Admin_Model_DbTable_Inv extends Zend_Db_Table_Abstract
{
    protected $_name = 'test';
    public function updateproductstock($qty,$pid)
    {
        $data = array(
            'stock' => 'stock - 2'
        );
        $this->_db->update($this->_name, $data, product = '.$pid.');
    } 
}

Here,I want to subtract my qty from stock.For example 2 will be qty. qty will be dynamic data .How can i use zend update here. kindly help.

Updated my post

4
  • $db-> refers to a database object (class). Please can you post the class to which this piece of code refers? Or at least the 'update' function Commented Oct 4, 2011 at 10:35
  • @luc, I agree the question is a little vagueness but with the reference to zend framework you can imagine that it refers to framework.zend.com/manual/en/zend.db.adapter.html and his update method. Commented Oct 4, 2011 at 10:41
  • @LukeCoulton This is Zend Framework, update is a method from Zend_Db. Commented Oct 4, 2011 at 11:43
  • Apologies, I missed the zend-framework tag. I use the zend studio purely for programming. Commented Oct 4, 2011 at 11:56

2 Answers 2

2
 class Admin_Model_DbTable_Inventory extends Zend_Db_Table_Abstract
 {
     protected $_name = 'test';

     public function updateproductstock($qty,$pid)
     {
         $data = array(
             'stock' => new Zend_Db_Expr('stock - '.$qty);
         );
         $this->_db->update($this->_name, $data, product = '.$pid.');
     }
 }
Sign up to request clarification or add additional context in comments.

5 Comments

Actually i want to subtract the stock value from qty in test table
Well then read the current stock value from the database, and use the code of my answer (I am going to edit it)
Yes , But suppose if i have 10 products in my cart, I will loop this updateproductstock function .Then it will take some time to execute. This will not be effective then.
sorry for my mistake.I gave it for only example...will update the post now
Ok I finally got what you wanted to do. See my edit, maybe a Zend_Db_Expr will do, it will prevent the string from being escaped by Zend_Db (so it will be a valid SQL expression).
0
 class Admin_Model_DbTable_Inv extends Zend_Db_Table_Abstract
 {
     protected $_name = 'test';

     public function updateproductstock($qty)
     {           
         $r = $this->fetchRow($this->select()->where('product  = ?', 1));
         $r->stock -= $qty;
         $r->save();
         $data = array(
             'stock' => 'stock - '.(string) $qty;
         );
         $this->_db->update($this->_name, $data, "product = '1'");
     }
 }

ps: All the variables should be prefix with $

3 Comments

$values = array('stock' => 'stock - 2'); $oDb->update('test', $values, 'product_id = 34'); this updates 0 value in current_stock, and when i run this $values = array('stock' => '41 -'.(string)2); $oDb->update('test', $values, 'product_id = 34'); it updates 41 as stock and it does not subtract the stock. SO this query is not working.
@Dinesh you are getting 0 probably because your column is INT, so if you fill it with a string, it will be converted to an integer (which will give here 0). See my answer, maybe it will work.
@Matthieu you're right, I don't understand as long as no variable could be recognized...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.