ZF2数据库配置问题

时间:2014-08-05 15:16:56

标签: php mysql zend-framework2

我的zend框架2项目涉及在线餐厅菜单。问题是,Pizza模块的视图index.pthml应该从名为pizza的数据库中检索披萨表中的所有数据,但数据不会显示。此外,当我尝试在addPizzaForm.php和add.phtml视图的帮助下通过表单添加新披萨时,没有数据添加到数据库中的表中。

我认为我的数据库配置有问题。

仅为记录我使用8080端口作为localhost。这是我的文件,请帮我找出我的代码有什么问题。

Global.php:

<?php
/**
 * Global Configuration Override
 *
 * You can use this file for overriding configuration values from modules, etc.
 * You would place values in here that are agnostic to the environment and not
 * sensitive to security.
 *
 * @NOTE: In practice, this file will typically be INCLUDED in your source
 * control, so do not include passwords or other sensitive information in this
 * file.
 */

return array(
      'db' => array(
                 'driver' => 'Pdo',
                 'dsn' => 'mysql:dbname=pizza;host=localhost', 
                 'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES     \'UTF8\''),),
      'service_manager' => array( 
                             'factories' => array('Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory')),
);

local.php:

<?php 
return array(
       'db' => array( 
                  'username' => 'root',
                  'password' => '',)
             );

Module.php:

<?php

namespace Pizza;
use Pizza\Model\Pizza; 
use Pizza\Model\PizzaTable; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

class Module {

public function getAutoLoaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoLoader' => array(__DIR__.'/autoload_classmap.php',),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,),
            ),
            );
}

public function getConfig()
{
    return include __DIR__.'/config/module.config.php';
}

public function getServiceConfig() 
{ 
    return array('factories' => array( 
                                'Pizza\Model\PizzaTable' => function ($sm) 
                                { 
                                    $tableGateway = $sm->get('PizzaTableGateway');
                                    $table = new PizzaTable($tableGateway); 
                                    return $table; 
                                },

                                'PizzaTableGateway' => function ($sm) 
                                { 
                                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
                                    $resultSetPrototype = new ResultSet();
                                    $resultSetPrototype->setArrayObjectPrototype(new Pizza()); 
                                    return new TableGateway('pizza',$dbAdapter, null, $resultSetPrototype);
                                } 
                                )
                 );

}



}

Pizzatable.php:

<?php

namespace Pizza\Model;
use Zend\Db\TableGateway\TableGateway; 

class PizzaTable{
protected $tableGateway;

public function __construct(TableGateway $tableGateway) 
{
    $this->tableGateway=$tableGateway;
}

public function find_all() 
{
    //select • FRom Pizza 
    return $this->tableGateway->select();
} 

public function find_by_id($id=0) 
{
    $id = (int)$id;
    $result=$this->tableGateway->select(array('id'=>$id)); 
    $row=$this->tableGateway->current(); 
    if ($row)
    {
        return $row;
    }
    else
    {
        return null;
    }
}

public function save(Pizza $pizza)
{
    $data = array ('name'  => $pizza->name, 
                'ingredients'  => $pizza->ingredients,
                'smallprice' =>  $pizza->smallprice,
                'bigprice' =>  $pizza->bigprice,
                'familyprice'  =>  $pizza->familyprice,
                'partyprice'  =>  $pizza->partyprice,
                );
    $id = (int) $pizza->id; 
    if(Sid == 0) 
    {
        // insert a new record
        $this->tableGateway->insert($data); 
    }
    else 
    {
        // update a new record
        if($this->find_by_id($id)) 
        {
            $this->tableGateway->update($data , array('id'=>$id));
        } 
        else
        {
            throw new \Exception("the pizza with the id = {Sid} could not be found in database"); 

        } 
    }
}

public function delete($id=0)
{
    $this->tableGateway->delete(array('id' => (int)$id));
}

}

0 个答案:

没有答案