0

我想我只是工作太久而且很累。我有一个使用 Zend 框架的应用程序,我在其中显示来自数据库的俱乐部列表。然后,我希望用户能够单击俱乐部并将俱乐部的 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

您可以将俱乐部 ID 作为视图中的 href 作为链接的 URL 放入 - 例如/controllername/club/12,然后使用以下命令在控制器中获取该信息:

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

如果没有给出参数,'false' 将是默认值。(int) 是一个很好的做法,可以确保你得到一个数字(或者 0,如果它是其他一些非数字字符串)。

于 2012-04-11T13:35:45.693 回答