2013-08-29 121 views
0

你好,我有一個問題,我不知道我的錯誤在哪裏。我創建了一個名爲Phonebook的模塊,當我想從數據庫中獲取結果時,我得到一個空白頁面。Zend Framework 2服務管理器

這裏是我的代碼示例:

控制器:

namespace Phonebook\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

use Phonebook\Model\Phonebook; 

class PhonebookController extends AbstractActionController 
{ 
    protected $phonebookTable; 

    /** 
    * Get Model Table 
    */ 

    public function getPhonebookTableGateway() 
    { 
     if (!$this->phonebookTable){ 
      $sm = $this->getServiceLocator(); 
      $this->phonebookTable = $sm->get('Phonebook\Model\PhonebookTable'); 
     } 
     return $this->phonebookTable; 
    } 


    /** 
    * Index Action 
    * 
    * @see \Zend\Mvc\Controller\AbstractActionController::indexAction() 
    */ 
    public function indexAction() 
    { 
     return new ViewModel(array(
      'phonebook' => $this->getPhonebookTableGateway()->fetchAll() 
     )); 
    } 

注:我不顯示控制器的全部源代碼,因爲它是如此之大。

Module.php服務管理

namespace Phonebook; 


use Phonebook\Model\Phonebook; 
use Phonebook\Model\PhonebookTable; 

use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Phonebook\Model\PhonebookTable' => function($sm) { 
        $tableGateway = $sm->get('PhonebookTableGateway'); 
        $table = new PhonebookTable($tableGateway); 
        return $table; 
       }, 
       'PhonebookTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Phonebook()); 
        return new TableGateway('phonebook', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ); 
    } 

PhonebookTable

<?php 
namespace Phonebook\Model; 

// Get Zend Table Gateway 

use Zend\Db\TableGateway\TableGateway; 


class PhonebookTable 
{ 
    protected $tableGateway; 

    /** 
    * Constructor 
    * 
    * @return Inject Dependency in TableGateway. 
    */ 
    public function __construct(TableGateway $tableGateway) 
    { 
     $this->tableGateway = $tableGateway; 
    } 

    /** 
    * Fetch all 
    * 
    * Method fetch all article from database 
    * 
    * @return resource 
    */ 
    public function fetchAll() 
    { 
     $resultSet = $this->tableGateway->select(); 
     return $resultSet; 
    } 

} 

電話簿

<?php 
namespace Phonebook\Model; 

class Phonebook 
{ 
    /** 
    * Item ID 
    * @var int 
    */ 
    public $id; 
    /** 
    * First Name 
    * @var str 
    */ 
    public $firstname; 
    /** 
    * Last Name 
    * @var str 
    */ 
    public $lastname; 
    /** 
    * Phone 
    * @var Str 
    */ 
    public $phone; 
    /** 
    * City 
    * @var str 
    */ 
    public $city; 

    /** 
    * Exchange Data 
    * 
    * @return array 
    */ 
    public function exchangeArray($data) 
    { 
     $this->id = (!empty($data['id'])) ? $data['id'] : null; 
     $this->first_name = (!empty($data['first_name'])) ? $data['first_name'] : null; 
     $this->last_name = (!empty($data['last_name'])) ? $data['last_name']: null; 
     $this->phone = (!empty($data['phone'])) ? $data['phone'] : null; 
     $this->city = (!empty($data['city'])) ? $data['city'] : null; 
    } 

} 

查看

<tbody> 
    <?php foreach ($phonebook as $item) :?> 
     <tr> 
      <td> <?php echo $this->escapehtml($item->id);?> 
      <td> <?php echo $this->escapehtml($item->firstname);?> 
      <td> <?php echo $this->escapehtml($item->lastname);?> 
      <td> <?php echo $this->escapehtml($item->phone);?> 
      <td> <?php echo $this->escapehtml($item->city);?> 
     </tr> 
    <?php endforeach;?> 
</tbody> 

當我從控制器刪除服務管理器,頁面呈現成功沒有數據庫的結果。

+0

在你看來,$ phonebook-> count()的結果是什麼? – crowebird

+0

我沒有看到任何東西只是毛坯白頁。我有db table'phonebook'中的記錄。讓我檢查一下'計數'沒有什麼結果。 – Ivan

+0

再次沒有什麼是render.:/ – Ivan

回答

0

你需要phoneBook表;所以在控制器中你應該有:

$phoneBookTable = $this->getServiceLocator()->get('PhonebookTable'); 

$allPhonebook=$phoneBookTable->fetchAll();