views:

49

answers:

1

Hello I need help with displaying data from 2 table.

So i Have 2 models:

//Klasy.php

class Model_Klasy extends Zend_Db_Table_Abstract
{
    protected $_name = 'klasy';
    protected $_primary = 'id';
    protected $_referenceMap = array(
    'Nauczyciele' => array(
        'columns'       =>  array('Wychowawca'),
        'refTableClass' =>  'Model_Nauczyciele',
        'refColumns'    =>  array('id')
    )
    );

    public function listaKlas()
    {
    $dane = $this->select();
    return $this->fetchAll();
    }
}

and

//Nauczyciele.php
    class Model_Nauczyciele extends Zend_Db_Table_Abstract
    {
        protected $_name = 'nauczyciele';
        protected $_dependentTables = 'Model_Klasy';
    }

In Controller have that code:

public function listaAction()
    {
        $modelLista = new Model_Klasy();
    $this->view->listaKlas = $modelLista->listaKlas();
    }

and in View this:

<b>Lista Klas:</b>
<table>
    <tr>
    <th>Klasa</th>
    <th>Rok rozpoczęcia nauki</th>
    <th>Wychowawca</th>
    </tr>
    <tr><?php echo $this->partialLoop('partials/ListaKlas.phtml', $this->listaKlas); ?></tr>
</table>

and partial file ListaKlas.phtml:

<tr>
    <td><?php echo $this->nazwa; ?></td>
    <td><?php echo $this->rok_rozpoczecia; ?></td>
    <td>
    <?php
    echo $this->Wychowawca;
    ?>
    </td>
    <td style="width: 5%">
    <a href="<?php echo $this->baseUrl() . '/klasy/edytuj/id/';
            echo $this->id ;?>">Edytuj
    </a>
    </td>
    <td style="width: 5%">
    <a href="<?php echo $this->baseUrl() . '/klasy/usun/id/';
            echo $this->id ;?>">Usuń
    </a>
    </td>
</tr>

from Table Nauczyciele i want display 2 columns but dont know how. I know about method findParentRow but dont know where use it and how render data from second table. In my case i see only ID from table Klasy.

+1  A: 

You should have a detailed look at the way domain objects and database tables are mapped in the Zend Framework. For example, look at the quickstart: http://framework.zend.com/manual/en/learning.quickstart.create-model.html

There is a model (Model_Klasy in your case), a mapper connecting models to DbTables (Model_KlasyMapper) and a class representing your Db table (Model_Klasy_DbTable_Klasy).

In the domain layer the model is just the representation of your model without knowlegde of the database layer. The dbTable class knows how to connect to the databse and the mapper connects both classes to each other. The FetchAll() method should be inside the mapper, not the Model_Klasy itself.

In the mapper, the fetchAll function might look like this:

public function fetchAll()
{
    $resultSet = $this->getDbTable()->fetchAll();
    $entries   = array();
    foreach ($resultSet as $row) {
        $entry = new Application_Model_Klasy();
        $entry->setId($row->id)
              ->setName($row->name);
        $entries[] = $entry;
    }
    return $entries;
}

In this function you can use the $resultSet and $row to fetch additional information from related tables. The manual page Zend_Db_Table Relationships tells more about that: http://framework.zend.com/manual/en/zend.db.table.relationships.html

The $row variable in the foreach loop of $resultSet is a Zend_Db_Table_Row and can find related information. Calling $row->findParentRow('Model_DbTable_Nauczyciele') will fetch the relation.

Jurian Sluiman
Thanks for information. I must now read more about this. In book from which i learned ZF thet write that like in my case. I must change it.
WooCaSh
I change my model to this method what u show me. Thanks for help
WooCaSh