nielsv
nielsv

Reputation: 6810

Read multiple rows from column + zend framework

I want to load all my rows of column "name" in my table "districts".

I can already read the first row but not more then that.

My Controller:

public function showdataAction()
    {
        $districtMapper = new Backoffice_Model_DistrictMapper();
        $array = $districtMapper->read();    
        Zend_Debug::dump($array);     
    }

My DistrictMapper:

public function read()
    {
        $table = $this->_dbTable;    
        $columns = array('wijk' => 'wijk',   'coords' => 'coords', );    
        $select = $table->select() ->from($table, $columns) ->order('wijk ASC');
        if ($row = $table->fetchRow($select)) {
           return $row->toArray();
        }    
       throw new Exception('The requested data cannot be found');
    }

In my Controller I have Zend_Debug::dump($array) and the result is:

array(2) {
  ["wijk"] => string(10) "Binnenstad"
  ["coords"] => string(2186) "3.72448685517794,51.0601522198842,
                 0 3.72577413282654,51.0597215800093,0 3.72594328459339,
                 51.0600395135711,0 3.72699385985136, 51.060876600363,
                 0 3.72764765694006,51.0608474287073,
                 0 3.7281167878913,51.0605006616679,0 3.72955689560896,
                 51.059999738927"
}

Only the first row ... How can I can convert the read function so I can load all the rows?

Upvotes: 1

Views: 1041

Answers (2)

RockyFord
RockyFord

Reputation: 8519

public function read()
    {
        $table = $this->_dbTable;    
        $columns = array('wijk' => 'wijk',   'coords' => 'coords', );    
        $select = $table->select() ->from($table, $columns) ->order('wijk ASC');
        //try/catch might make more sense as fetchAll() returns an object or an exception.
        $result = $table->fetchAll($select)

        return $result->toArray();

    }

Change fetchRow() to fetchAll(). fetchRow() returns one row object or null, fetchAll() returns a rowset object (array of row objects) or an exception/fatal error.

Upvotes: 1

Arun Killu
Arun Killu

Reputation: 14233

if ($row = $table->fetchRow($select)) {
           return $row->toArray();
       }

change that portion of code to

return $your_database_object->fetchAll($select);

Upvotes: 0

Related Questions