繁体   English   中英

zend framework1中一些代码的问题

[英]issue with some codes in zend framework1

我正在学习本教程: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> 

问题:

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

  2. IndexController.php

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

    b。 $this->view->albums = $albums->fetchAll(); 这是什么意思: $this->view->albums 为什么不使用$this->albums呢?

  3. index.phtml

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

    b。 为什么在这里使用foreach($this->albums as $album) 不是$this->view->albums如IndexController.php?

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

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

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

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

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

$this->view->albums = $ablums->fetchAll(); 是一种将从$albums->fetchAll()获得的值分配给Zend_View对象以在视图脚本中显示的$albums->fetchAll() 这些值可以在视图脚本中作为echo $this->albums进行回显,或者更有可能将它们与foreach循环然后显示。

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

实际上,这很难轻松固定,您可以在Zend_View帮助器参考中找到一个基本定义,只需向下滚动直到看到它即可。 Zend_Controller参考路由器部分中可以找到一个更好的示例。 本质上, url()帮助器是在视图脚本中创建链接的首选方法,它不是唯一的方法,有时也不是最好的方法。

为什么在这里使用foreach($ this-> albums as $ album)? 不是$ this-> view-> albums如IndexController.php?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM