2010-11-15 8 views
1

J'apprécierais que quelqu'un puisse expliquer pourquoi cette requête retourne des valeurs nulles. This query works fine when I use the fetchRow() method.Zend DB: valeurs nulles

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; 
} 

EDIT:

Je tente d'accéder à l'ensemble de lignes comme ceci:

$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

Etes-vous sûr qu'il retourne NULL? fetchAll renvoie un tableau, tandis que fetchRow renvoie le premier ROW. –

+0

Je reçois des valeurs nulles Lorsque j'essaie de récupérer des valeurs de colonnes, en particulier lorsque j'essaie de récupérer 'location_name'. – Fortisimo

Répondre

1

fetchRow() retourne un objet de Zend_Db_Table_Row_Abstract. fetchAll() retourne un tableau.

0

La méthode Zend_Db_Table_Rowset_Abstract de classe toArray se comporte de manière non documentée. Si vous n'avez pas parcouru les lignes avant de l'appeler, il ne se comportera pas comme prévu.

Donc, la solution serait de itérer directement pensé que le rowset (Il peut itérer):

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

Hope this helps.

+0

Je suis intéressé par ce comportement étrange. Qu'Est-ce que c'est? J'ai toujours utilisé la méthode toArray et j'ai pu accéder à tous les champs de la table à l'aide des clés de tableau en tant que telles: row ['id'] –

0

Je l'ai réparé les gars. Juste dû vérifier un tableau d'abord et se débarasser du contrôle de compte de deuxième rangée et de son autre déclaration.

 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 
       } 
      } 

     }