zend框架1中的一些代码问题

时间:2013-04-25 03:51:12

标签: php zend-framework

我正在学习本教程:Zend Framework 1.11入门

  

http://akrabat.com/wp-content/uploads/Getting-Started-with-Zend-Framework.pdf

,第12页,“列出相册”

  1. D:\program files (x86)\Zend\Apache2\htdocs\zf-tutorial\application\models\DbTable\Albums.php

    <?php
    
    class Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract {
    
        protected $_name = 'albums';
    
        public function getAlbum($id)
        {
            ...
        }
        public function addAlbum($artist, $title)
        {
        ...
        }
    
        public function updateAlbum($id, $artist, $title)
        {
            ...
        }
        public function deleteAlbum($id)
        {
            ...
        }
    
    
    }
    

    1. D:\program files (x86)\Zend\Apache2\htdocs\zf-tutorial\application\controllers\IndexController.php

      <?php
      
      class IndexController extends Zend_Controller_Action  
      {
          public function indexAction() 
          $albums = new Application_Model_DbTable_Albums();
               $this->view->albums = $albums->fetchAll();
          }    
      }
      

      1. D:\program files (x86)\Zend\Apache2\htdocs\zf-tutorial\application\views\scripts\index\index.phtml

         <?php
         $this->title = "My Albums";
         $this->headTitle($this->title);
         ?>
         <p><a href="<?php echo $this->url(array('controller'=>'index',
         'action'=>'add'));?>">Add new album</a></p>
         <table>
         <tr>
         <th>Title</th>
         <th>Artist</th>
         <th>&nbsp;</th>
         </tr>
         <?php foreach($this->albums as $album) : ?>
         <tr>
         <td><?php echo $this->escape($album->title);?></td>
         <td><?php echo $this->escape($album->artist);?></td>
         <td>
         <a href="<?php echo $this->url(array('controller'=>'index',
         'action'=>'edit', 'id'=>$album->id));?>">Edit</a>
         <a href="<?php echo $this->url(array('controller'=>'index',
         'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
         </td>
         </tr>
         <?php endforeach; ?>
         </table>
        

      2. 问题:

        1. Albums.php,为什么我们需要这个:protected $_name = 'albums';

        2. IndexController.php

          一个。 $albums = new Application_Model_DbTable_Albums();我们没有使用include / require“Albums.php”,我们怎么能使用这个类:Application_Model_DbTable_Albums

          $this->view->albums = $albums->fetchAll();这意味着什么:$this->view->albums?为什么不使用$this->albums

        3. index.phtml

          一个。 $this->url(array('controller'=>'index','action'=>'add')),我在哪里可以查看此方法:$this->url()

          湾为什么我们在这里使用foreach($this->albums as $album)?不像IndexController.php中那样$this->view->albums

1 个答案:

答案 0 :(得分:0)

  

Albums.php,为什么我们需要这个:protected $ _name ='albums';?

在Zend Framework(Application_Model_DbTable_Albums)中使用DbTable模型时,受保护的成员$_name必须是与模型关联的数据库表的名称。 DbTable模型实际上将成为该表的数据库适配器。

  

$ albums = new Application_Model_DbTable_Albums();我们没用   include / require“Albums.php”,我们怎么能使用这个   类:Application_Model_DbTable_Albums

符合预期命名约定且属于预定义资源的类和文件由Zend_Loader自动自动加载。类Application_Model_DbTable_Albums和文件Application/Model/DbTable/Albums.php都符合命名约定,属于预定义的资源。

  

$ this-&gt; view-&gt; albums = $ albums-&gt; fetchAll();这是什么意思:   $这个 - &GT;查看 - &GT;相册?为什么不使用$ this-&gt;专辑?

$this->view->albums = $ablums->fetchAll();是将从$albums->fetchAll()获取的值分配给Zend_View object以便在视图脚本中显示的简便方法。这些值可以在视图脚本中以echo $this->albums形式回显,或者更有可能使用foreach进行循环,然后显示。

  

$ this-&gt; url(array('controller'=&gt;'index','action'=&gt;'add')),我在哪里可以   检查此方法:$ this-&gt; url()?

这实际上有点难以确定,你会在Zend_View helper reference中找到一个基本定义,只需向下滚动直到你看到它。在Zend_Controller reference router section中可以找到一个更好的例子。基本上url()帮助器是在视图脚本中创建链接的首选方法,它不是唯一的方法,有时不是最好的方法。

  

为什么我们在这里使用foreach($ this-&gt;专辑作为$ album)?不   $ this-&gt; view-&gt;如IndexController.php中的相册?

在控制器中,我们将数据分配给视图脚本(index.phtml)中的视图对象$this->view->data = $data,我们在视图对象中,因此我们只需要访问提供的数据<?php echo $this->data ?>。在$this->albums的情况下,数据是行集对象(Zend_Db_Row对象的数组),可以使用foreach循环进行迭代。可以使用此方法将大多数有效数据类型分配给视图。

这是对这些概念的一个非常简单的概述,我希望它有所帮助。

相关问题