0

仕事が長すぎて疲れていると思います。データベースからクラブのリストを表示するZendFrameworkを使用するアプリケーションがあります。次に、ユーザーがクラブをクリックして、クラブのIDを別のページに投稿して詳細情報を表示できるようにします。

クラブコントローラーは次のとおりです。

class ClubsController extends Zend_Controller_Action
{

    public function init()
    {

    }

    public function indexAction()
    {
        $this->view->assign('title', 'Clubs');
        $this->view->headTitle($this->view->title, 'PREPEND');
        $clubs = new Application_Model_DbTable_Clubs();
        $this->view->clubs = $clubs->fetchAll();
    }
}

モデル:

class Application_Model_DbTable_Clubs extends Zend_Db_Table_Abstract
{

    protected $_name = 'clubs';

    public function getClub($id) {
        $id = (int) $id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Count not find row $id");
        }
        return $row->toArray();
    }
}

景色:

<table>
    <?php foreach($this->clubs as $clubs) : ?>
    <tr>
        <td><a href=''><?php echo $this->escape($clubs->club_name);?></a></td>
        <td><?php echo $this->escape($clubs->rating);?></td>
    </tr>
    <?php endforeach; ?>
</table>

zendフレームワークでどのように行われるかについて混乱していると思います。

4

3 に答える 3

2

あなたの見解ではこれを行う

<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
                    'controller' => 'club-description',
                    'action' => 'index',
                    'club_id' => $clubs->id
                    ));?>">
...

そうすれば、ClubDescriptionコントローラーのインデックスアクションでclub_idパラメーターを使用できるようになります。あなたはこのようにそれを得る$this->getRequest()->getParam('club_id')

于 2012-04-11T15:25:07.610 に答える
1

例:

class ClubsController extends Zend_Controller_Action
{

    public function init()
    {

    }

    public function indexAction()
    {
        $this->view->assign('title', 'Clubs');
        $this->view->headTitle($this->view->title, 'PREPEND');
        $clubs = new Application_Model_DbTable_Clubs();
        $this->view->clubs = $clubs->fetchAll();
    }

   public function displayAction() 
   {
       //get id param from index.phtml (view)
       $id = $this->getRequest()->getParam('id');
       //get model and query by $id
       $clubs = new Application_Model_DbTable_Clubs();
       $club = $clubs->getClub($id);
       //assign data from model to view [EDIT](display.phtml)
       $this->view->club = $club;
       //[EDIT]for debugging and to check what is being returned, will output formatted text to display.phtml
       Zend_debug::dump($club, 'Club Data');

    }
}

[編集]display.phtml

<!-- This is where the variable passed in your action shows up, $this->view->club = $club in your action equates directly to $this->club in your display.phtml -->
<?php echo $this->club->dataColumn ?>

ビューindex.phtml

<table>
    <?php foreach($this->clubs as $clubs) : ?>
    <tr>
    <!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
    <!-- this method of passing a url is easy to understand -->
        <td><a href='/index/display/id/<?php echo $clubs->id; ?>'><?php echo $clubs->club_name;?></a></td>
        <td><?php echo $clubs->rating;?></td>
    </tr>
<?php endforeach; ?>

url()ヘルパーを使用したビューの例

<table>
        <?php foreach($this->clubs as $clubs) : ?>
        <tr>
        <!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
        <!-- The url helper is more correct and less likely to break as the application changes -->
            <td><a href='<?php echo $this->url(array(
                'controller' => 'index',
                'action' => 'display',
                'id' => $clubs->id
             )); ?>'><?php echo $clubs->club_name;?></a></td>
            <td><?php echo $clubs->rating;?></td>
        </tr>
    <?php endforeach; ?>
</table>

[編集]モデル内の現在のgetClub()メソッドの構築方法では、を使用してデータにアクセスする必要がある場合があります$club['data']->toArray()これは、戻り値からを削除することで修正できます。
まだ行っていない場合は、.htaccessファイルに次の行を追加して画面上のエラーメッセージをアクティブにすることができますSetEnv APPLICATION_ENV development。あなたが提供した情報を使用して、display.phtml生きていることを確認してくださいapplication\views\scripts\club-description\display.phtml(これは正しいと確信しています、ZFはいくつかのキャメルケース名を面白い方法で処理します)

于 2012-04-12T06:56:23.683 に答える
0

ビュー内のhrefとしてリンク先のURLにクラブIDを入力できます。たとえば、次の/controllername/club/12コマンドを使用して、コントローラーでその情報を取得します。

$clubId = (int) $this->_getParam('club', false);

パラメータが指定されていない場合、「false」がデフォルト値になります。(int)は、数値(または、他の非数値文字列の場合は0)を確実に取得するための良い方法です。

于 2012-04-11T13:35:45.693 に答える