Zend Generic View Helper to display any result set DB-> fetchAll ()

Is it possible to write a Zend View helper that could present the result set of any fetchAll () operation as a common table ?.

My model code is as follows

class Model_DbTable_XWZ extends Zend_Db_Table_Abstract
{
    protected $_name = 'xwz';
    protected $_primary = 'id';

    public function getA()
    {
        $sql = $this->select()
        ....
        return $this->fetchAll($sql);
    }

    public function getB()
    {
        $sql = $this->select()
        ...... 
        return $this->fetchAll($sql);

but instead of using Zend_Debug :: Dump () to view the results, it would be useful to use a generic view helper.

class Zend_View_Helper_DisplayGenericTableHelper extends Zend_View_Helper_Abstract {

    public $view;

    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }

    public function displayGenericTableHelper($result)
    {
        ....??        
    }
}

Something like, but I'm not sure how to determine the column names from the $ result object.

+3
source share
2 answers

You can use a function Zend_Db_Table_Row_Abstract::toArray(), for example:

// in view helper
public function displayGenericTableHelper(Zend_Db_Table_Rowset_Abstract $rowset) {
    $table = '<table><tr>';
    foreach(array_keys($rowset->current()->toArray()) as $column) {
        $table .= '<th>'.$column.'</th>';
    }
    foreach($rowset as $row) {
        $table .= '</tr><tr>';
        foreach($row->toArray() as $content) {
            $table .= '<td>'.$content.'</td>';
        }
    }
    return $table.'</tr></table>';
}

Use Case:

// in a view
<?= $this->displayGenericTableHelper($this->model_data) ?>

alternative call:

// in controller
$model_data = $your_model->getA();
$view->your_table = $view->displayGenericTableHelper($model_data);

// in view:
<?= $this->your_table ?>

Improvement Point: Use the part to save HTML from your assistant.

+4
source

Source: https://habr.com/ru/post/1746005/


All Articles