1

I would appreciate it if some one could explain why this query is returning null values. This query works fine when I use the fetchRow() method.

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:

I try to access the rowset like so:

    $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;
            }
        }
   }
2
  • Are you sure it's returning NULL? fetchAll returns an array, while fetchRow returns the first ROW. Commented Nov 15, 2010 at 17:01
  • I get null values When I try to retrieve some column values, especially when I try to retrieve 'location_name'. Commented Nov 15, 2010 at 17:04

3 Answers 3

1

fetchRow() returns an object of Zend_Db_Table_Row_Abstract. fetchAll() returns an array.

Sign up to request clarification or add additional context in comments.

Comments

0

The Zend_Db_Table_Rowset_Abstract class toArray method, behaves in wierd manner that is not documented. If you haven't iterated through the rows before calling it, it won't behave like expected.

So the solution would be to directly iterate thought the rowset (It can be iterated):

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

Hope this helps.

1 Comment

I'm interested in this weird behavior. What is it? I've always just used toArray method and was able to access all the fields from the table using array keys as such: row['id']
0

I've fixed it guys. Just had to check for an array first and get rid of the second row count check and its else statement.

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

        }

Comments

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.