ZF Database

From Triangle Wiki

Jump to: navigation, search

Contents

Introduction

Some simple examples of how to handle Zend Framework DB Table Queries.

Select

  • Using the shortcut Find operation to find a Row by a primary key

  1. // Query to check whether the Answer is correct for the question ID
  2. $class = $this->social->config->path->models . 'Trivia';
  3. $table = new $class();
  4. $result = $table->find($this->_getParam('qid'));
  5. $triviaRow = $result->current();
  • Fetching all the returned rows

  1. // Query to get the user Favourite team
  2. $class = $this->social->config->path->models . 'UserTeamView';
  3. $table = new $class();
  4. $where = array('user_id = ?' => $this->social->uId);
  5. $order = 'order';
  6. $row = $table->fetchRow($where, $order);

Insert

  • Using the shortcut Find operation to find a Row by a primary key

  1. // Query to insert Users who are interacting with the application type = User
  2. $class = $this->social->config->path->models . 'User';
  3. $table = new $class();
  4.  
  5. $data = array(
  6. 'id' => $this->social->uId,
  7. 'network' => $this->social->name,
  8. 'type' => $this->social->type,
  9. 'proxied_email' => null,
  10. 'status' => 'User'
  11. );
  12.  
  13. $result = $table->insert($data);

Update

  • Using the shortcut Find operation to find a Row by a primary key

  1. // Query to update the users total points
  2. $table = new Default_Model_DBTable_UserDetail;
  3. $data = array('points_trivia' => new Zend_Db_Expr('`points_trivia` + ' . $pointsGained));
  4. $where = $table->getAdapter()->quoteInto('id = ?', $this->_getParam('fb_sig_user'));
  5. $updateUser = $table->update($data, $where);
  6.  
  7. // Instatiate the Model
  8. $table = new Default_Model_DBTable_Account();
  9.  
  10. // Query to update the Account table
  11. $data = array(
  12. 'reference_id' => $dataArray['data']['reference'],
  13. 'invoice' => $dataArray['data']['invoice'],
  14. 'vat' => $dataArray['data']['vat']
  15. );
  16.  
  17. $where = $table->getAdapter()->quoteInto('id = ?', $this->_getParam('id'));
  18. $result = $table->update($data, $where);

External Links

Navigation

Setup

User Module

Data Module

  • Database

Edit Navigation

Personal tools