Zend Framework 2服务管理器

时间:2013-08-29 18:53:33

标签: php zend-framework2

您好我有问题,我不确定我的错误在哪里。我创建了一个名为Phonebook的模块,当我想从数据库中获取结果时,我得到一个空白页面。

以下是我的代码示例:

控制器:

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>

当我从Controller中删除服务管理器时,页面成功呈现而没有数据库结果。

1 个答案:

答案 0 :(得分:0)

你需要phoneBook表;所以在控制器中你应该:

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

$allPhonebook=$phoneBookTable->fetchAll();