簡體   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