2010-11-15 78 views
1

如果有人能解釋爲什麼這個查詢返回空值,我將不勝感激。 This query works fine when I use the fetchRow() method.Zend DB:空值

public function getCustomerRowWithAddress($customer_id) 
{ 

    $select = $this->select()->setIntegrityCheck(false); 

    $select->from('irh_TV.irh_site_location AS SL', array('name as location_name', 'start_date', 'end_date', 'service_pack_id', 'stb_id')); 
    $select->joinLeft('irh_TV.irh_customers AS C', 'C.customer_id = SL.customer_id AND C.active = 1 AND C.site_id = SL.site_id', 
     array('customer_id','surname', 'title', 'site_id', 'first_name', 'company', 'business_no', 'home_no', 'mobile_no', 'email', 'address_id')); 
    $select->joinLeft('irh_TV.tv_stb_data AS TV', 'TV.site_id = SL.site_id AND SL.stb_id = TV.id', array('user_id as mac_address')); 
    $select->joinLeft('irh_TV.irh_address AS AD', 'C.address_id = AD.id', array('address_line1', 'address_line2', 'town', 'county', 'country', 'postcode')); 
    $select->where("SL.customer_id = $customer_id"); 
    //if($_REQUEST['d'] == 1) { echo $select; } 
    _syslog($select); 
    //$_rows = $this->fetchRow($select); 
    $_rows = $this->fetchAll($select); 

    return $_rows; 
} 

編輯:

我嘗試訪問該行集,像這樣:

$model  = _getModel(); 
    $table  = $model->getTable("irh_customers"); 
    $customer_address_row = $table->getCustomerRowWithAddress($id); 
    //$customer_address_row = $table->getCustomerRowsWithAddress($id); 
    //$this->_row = $customer_address_row ? $customer_address_row : $table->getRowById($id); 

    $row_count = count($customer_address_row); 
    if($row_count > 0){ 
     ///$rows = $this->_row->toArray(); 
     $this->exists = true; 
     $this->id = $id; 
     if($row_count > 1){ 

      //$array = $customer_address_row->toArray(); 
      foreach($customer_address_row->toArray() as $row){ 

       foreach($row as $k => $v){ 
        //if($k != 'stb_id' || $k != 'mac_address'){ 
         if(!isset($this->k[$k])){ 
          $this->k[$k] = $v; 
         } 
        /*}else if($k == 'stb_id'){ 
         $this->k['stb_id'][] = $v; 
        } 
        else if($k == 'mac_address'){ 
         $this->k['mac_address'][] = $v; 
        }*/ 
       } 
      } 
     }else{ 
      foreach($customer_address_row->toArray() as $k => $v) 
      { 
       _syslog($v); 
       $this->k[$k] = $v; 
      } 
     } 
    } 
+0

你確定它是否返回NULL? fetchAll返回一個數組,而fetchRow返回第一個ROW。 – 2010-11-15 17:01:45

+0

我得到空值當我嘗試檢索某些列值時,特別是當我嘗試檢索'location_name'時。 – Fortisimo 2010-11-15 17:04:25

回答

1

fetchRow()返回Zend_Db_Table_Row_Abstract的對象。 fetchAll()返回一個數組。

0

Zend_Db_Table_Rowset_AbstracttoArray方法,行爲方式錯誤,沒有記錄。如果在調用它之前沒有遍歷行,它將不會像預期的那樣行爲。

因此,解決辦法是直接迭代認爲行集(它可以重複):

foreach ($customer_address_row as $row) { 
    //Do whatever you need with the row here 
} 

希望這有助於。

+0

我對這種奇怪的行爲感興趣。它是什麼?我一直使用toArray方法,並能夠使用數組鍵訪問表中的所有字段,如row ['id'] – 2010-11-17 00:11:48

0

我修復了它們。只需要首先檢查一個數組,並擺脫第二行計數檢查和其他語句。

 if($row_count > 0){ 
     $rows = $this->_row->toArray(); 
     $this->exists = true; 
     $this->id = $id; 
     if(is_array($rows[0]) && isset($rows)){ 
      foreach($rows as $i => $row){ 

       foreach($row as $k => $v){ 
        //Whatever 
       } 
      } 

     }