php - 在 zf2 mvc 之外使用 Zend_Db zf2 模块

标签 php zend-db zend-framework2

我正在编写一个不基于 zf2 mvc 的 PHP 应用程序。

我只想使用 Zend_Db zf2 模块。我如何配置我的应用程序才能知道 如何在需要的地方找到 Zend_Db 相关的 PHP 文件?

我用 phyrus 下载了 zf2 Zend_db 模块,并安装在 vendor/zf2/php 位置。

我尝试使用以下命令将模块添加到包含路径:

set_include_path("../vendor/zf2/php".PATH_SEPARATOR.get_include_path());

我在目录 Model/ 中创建了与每个​​表相关的模型类文件(使用 zend-db-model-generator)。

我的主要应用程序包含以下内容:

use DrinkManagement\Model\DrinkTable;
use Zend\Db\Adapter\Adapter;


set_include_path("../vendor/zf2/php".PATH_SEPARATOR.get_include_path());

require_once('Model/DrinkTable.php');

/**
 @var DrinkManagement\Model\Drink
 */
$drinkTable=null;

$drinkTable = new DrinkTable();
$res=$drinkTable->getDrink(1);
echo var_export($res,1);

我的 DrinkTable 类:

namespace DrinkManagement\Model;

use Zend\Db\TableGateway\AbstractTableGateway,

class DrinkTable extends AbstractTableGateway
{
protected $table ='drink';
protected $tableName ='drink';

public function __construct(Adapter $adapter)
{
    $this->adapter = $adapter;
    $this->resultSetPrototype = new ResultSet(new Drink);

    $this->initialize();
}

public function fetchAll()
{
    $resultSet = $this->select();
    return $resultSet;
}

public function newSelect() {
    return new Select;
}

public function getSelect(&$select,$columnsArray=array()) 
{
    $select = new Select;
    return $select->from('drink')->columns($columnsArray);      
}

public function createIfNotExist($checkColumnsArray,$optionalColumns=array(),&$isRowCreated=null) {
        $rowset=$this->select($checkColumnsArray);
        $row = $rowset->current();
        $id=null;
        if ($row == null) {
            $allColumns=array_merge($checkColumnsArray,$optionalColumns);
            $affectedRows = $this->insert($allColumns);
            if ($affectedRows != 1) {
                throw new \Exception("error: could not add line to db");
            }
            $id=$this->lastInsertValue;
            $isRowCreated=true;
        } else {
            $id=$row->drink_id;
            $isRowCreated=false;
        }
        return $id;
}

//http://stackoverflow.com/questions/6156942/how-do-i-insert-an-empty-row-but-have-the-autonumber-update-correctly

public function createEmptyRow() {
    $row=array(
    'drink_id' => null
    );
    $affectedRows=$this->insert($row);
    if ($affectedRows != 1) {
        throw new \Exception("error: could not add empty row to db");
    }
    $id=$this->lastInsertValue;
    return $id;
}

public function getDrink($id)
{
    $id  = (int) $id;
    $rowset = $this->select(array('drink_id' => $id));
    $row = $rowset->current();
    if (!$row) {
        throw new \Exception("Could not find row $id");
    }
    return $row;
}

public function saveDrink(Drink $drink)
{
    $data = array(
                    'drink_type_id' => $drink->drink_type_id,
                    'drink_brand_id' => $drink->drink_brand_id,
                    'creation_timestamp' => $drink->creation_timestamp,
                );

    $id = (int)$drink->id;
    if ($id == 0) {
        $this->insert($data);
    } else {
        if ($this->getDrink($id)) {
            $this->update($data, array('drink_id' => $id));
        } else {
            throw new \Exception('Form id does not exit');
        }
    }
}

public function addDrink($drink_type_id, $drink_brand_id = null, $creation_timestamp = null)
{
    $data = array(            'drink_type_id' => $drink_type_id,
                    'drink_brand_id' => $drink_brand_id,
                    'creation_timestamp' => $creation_timestamp,
                );
    $affectedRows=$this->insert($data);
            if ($affectedRows != 1) {
        return null;
    }
    return $this->lastInsertValue;
        }

public function updateDrink($drink_id, $drink_type_id, $drink_brand_id, $creation_timestamp)
{
    $data = array(
                    'drink_type_id' => $drink->drink_type_id,
                    'drink_brand_id' => $drink->drink_brand_id,
                    'creation_timestamp' => $drink->creation_timestamp,
                        );
    $this->update($data, array(drink_id => $id));
}

public function deleteDrink($id)
{
    $this->delete(array('drink_id' => $id));
}

}

当我尝试执行我的主 php 应用程序时,我收到以下错误消息:

PHP Fatal error:  Class 'Zend\Db\TableGateway\AbstractTableGateway' not found in /Users/ufk/Documents/workspace/tux-drink/TuxDb/mysql/Model/DrinkTable.php on line 10

关于如何在不到处添加 require_once 的情况下解决问题的想法?

也许我可以使用另一个 zf2 组件来自动加载相关类?

最佳答案

从我从您的代码中看到的情况来看,您没有在任何地方包含与必要类(例如 AbstractTableGateway)对应的文件。将供应商路径设置为包含路径不会解决您的问题。

尝试手动添加您的依赖项会给您带来很多麻烦,因为您不仅需要手动添加您正在使用的类的依赖项,还需要为它们的依赖项(适配器、驱动程序等)添加依赖项

在我看来,您最好使用依赖管理器,例如 Composer用于管理您的依赖项及其自动加载。实际上,您可以在 composer.json(您必须在根项目目录中创建)中定义 zendframework\zend-db 包的依赖项,如下所示:

{
    "name": "Your project name",
    "description": "Your project description",
    "autoload": {
        "psr-0": {
            "Zend\\Db\\": ""
        }
    },
    "target-dir": "Zend/Db",
    "repositories": [
                {
                    "type": "composer",
                    "url": "https://packages.zendframework.com/"
                }
            ],
    "require": {
        "php": ">=5.3.3",
        "zendframework/zend-db":"2.0.*"
    }
}

将 Composer 安装到项目文件夹后,从命令行运行 php composer.phar install。 Composer 将为您生成自动加载文件 vendor/autoload.php,您可以包含该文件以自动加载依赖类。

例子:

<?php

// Always include autoload files when using vendor classes
include 'vendor/autoload.php';
require_once('Model/DrinkTable.php');

use DrinkManagement\Model\DrinkTable;
use Zend\Db\Adapter\Adapter;

//Don't forget declaring an adapter

$adapter = new Adapter(array(
        'driver' => 'Mysqli',
        'database' => 'test',
        'username' => 'dev',
        'password' => 'dev'
     ));
//Your constructor should include the adapter
$drinkTable = new DrinkTable('drinktable', $adapter);

//Do necessary stuff with the table
//....


echo var_export($res,1);


?>

注意:您可以为 zend-loader 声明一个依赖项,这样您就可以使用 Zend\Loader\StandardAutoloader 来自动加载您自己的类(假设您使用的是 PSR-0)

关于php - 在 zf2 mvc 之外使用 Zend_Db zf2 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13578349/

相关文章:

javascript - 如何使用异常输入将数据从表单提取到 session 中

php - 通过ajax检查php session

php - POST 请求中的嵌套参数/值

javascript - 当我试图阻止多次单击提交时,验证不起作用

php - 加载 javascript 和 php 脚本时无法显示加载图像

当数组mysql查询中的复选框值时选中Php表单?

php - 我应该从 Zend_Db 移植到哪个 native mysql 类? (无 PDO 或 Mysqli)

php - Zend 表单编辑和 Zend_Validate_Db_NoRecordExists

zend-framework - 左外连接如何在 Zend 框架上工作

translation - 如何从浏览器首选项在 ZF2 中设置语言环境?