using Zend_Db zf2 module outside of zf2 mvc

徘徊边缘 提交于 2019-12-05 22:29:50

From what I see from your code, you don't include anywhere the files corresponding to the necessary classes (eg. AbstractTableGateway). Setting the vendor path as an include path won't solve your problem.

Trying to add manually your dependencies will cause you many troubles as you'll have not only to add manually your dependecies for the classes you are using but also for the their dependencies (Adapters, Drivers etc.)

In my opinion, It would be better for you to use a dependency manager such as Composer for managing your dependencies & their auto-loading. You can actually define a dependency for the zendframework\zend-db package within the composer.json (which you have to create at the root project directory) as follows :

{
    "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.*"
    }
}

After installing composer to your project folder, run php composer.phar install from your command line. Composer will generate you the auto-loading file vendor/autoload.php that you can include for automatically loading your dependencies classes.

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


?>

Note: You can declare a dependency for zend-loader, so that you can use Zend\Loader\StandardAutoloader for auto-loading your own classes (assuming you're using PSR-0)

This is not my work, but Abdul Malik Ikhsan (ZF2 contributor) has a great blog post about using Zend/DB as a standalone component.

Like @yechabbi mentioned, you'll probably do yourself a lot of good to use Composer as your dependency manager.

It looks like you might be stumbling with where you're placing Zend-Db and what you're naming it. For example, in my working ZF2 installation, the Zend_Db component is located in vendor/zendframework/zendframework/library/Zend/Db. Using Composer with the correct require statements as Abdul quotes in his blog post should take care of all that for you:

"require": {
    "php": ">=5.3.3",
    "zendframework/zend-db": "2.*",
    "zendframework/zend-stdlib": "2.*"
}

Abdul then uses the following namespaces in his Table class

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\HydratingResultSet;

I don't know that this will fix all of your issues but it will get you on the right track I hope.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!