Yafa Su
Yafa Su

Reputation: 85

Zend Database Table getrow

I have a table of serial keys

+----+---------------------+------+
| id | product_key         | used |
+----+---------------------+------+
|  1 | 3C45-GH76-9V74-HLKJ |    1 |
|  2 | REDF-6KI9-00IU-4867 |    0 |
|  3 | 1111-1111-1111-1111 |    0 |
|  4 | 2222-2222-2222-2222 |    0 |
|  5 | 3333-3333-3333-3333 |    0 |
+----+---------------------+------+

I would like to get one row record that used=0 using zend framework. Then change to used=1 if that product_key had select.

I am confuse using Zend.

I think it's something like that, but how to assign "1" into used if that product key had been select?

$productKey = $this->_helper->model('ProductKeys')->fetchOne('SELECT * from product_keys WHERE used=0 LIMIT 1');

Upvotes: 0

Views: 180

Answers (1)

Awnage
Awnage

Reputation: 94

There does not appear a way to do it in a single simple query. Also, fetchOne only gets the first column of the first record. That helper would return just the ID, and not the product_key.

Option 1: Modify your ProductKeys model to get the key and set it as used:

class My_Model_ProductKeys extends Zend_Db_Table_Abstract
...
function getKeyAndMarkUsed()
{
  $select = $this->select();
  $select->where('used=?',1)->limit(1)->order('ID');
  $keyRow = $this->fetchRow();
  if ($keyRow){
    $this->update(array('used'=>1),'id='.$keyRow->ID);
    return $keyRow->id;
  }
  else{
    //no keys left! what to do??? Create a new key?
    throw new Exception('No keys left!');
  }
}

Then you would just:

$productKey = $this->_helper->model('ProductKeys')->getKeyAndMarkUsed();

Option 2: Make a database procedure to do the above functionality and call that instead.

Upvotes: 1

Related Questions