在 zf2 mvc 之外使用 Zend_Db zf2 模块

2024-04-02

我正在编写一个不基于 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());

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

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

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 http://www.getcomposer.org/用于管理您的依赖项及其自动加载。您实际上可以在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您可以包含它以自动加载依赖项类。

Example:

<?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)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 zf2 mvc 之外使用 Zend_Db zf2 模块 的相关文章

随机推荐