您如何在Zend Framework中使用关系数据库?

| 我正在使用zend框架编写应用程序,但是在使我的关系数据库模型正常工作时遇到了很多问题。我已经阅读了快速入门和文档多次,但仍不确定如何执行此操作。我张贴一张涉及不同表之间关系的图片,以避免不得不解释所有内容,因为英语不是我的母语,并且在尝试解释...复杂的事情时,我倾向于不清晰。 在名为planilla_users的表中,将表press_releases,social_networking,blog_posts,rss_feed,directorios,users和articulos都设置为外键。我以快速入门显示的方式对表模型进行了编码:
class Application_Model_DbTable_PlanillaUsers extends Zend_Db_Table_Abstract
{

protected $_name = \'planilla_users\';
protected $_referenceMap = array(
    \'User\' => array(
        \'columns\' => \'users_id\',
        \'refTableClass\' => \'Application_Model_DbTable_Users\',
        \'refColumns\' => \'id\'
    ),
    \'Articulo\' => array(
        \'columns\' => \'art_id\',
        \'refTableClass\' => \'Application_Model_DbTable_Articulos\',
        \'refColumns\' => \'id\'
    ),...etc
...其余的格式如下:
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{

protected $_name = \'users\';
protected $_dependentTables = array(\'Application_Model_DbTable_PlanillaUsers\'); 
我还有一个Planilla.php模型,其中包含所有设置程序和获取程序,用于存储/更新/检索/删除信息,但是...我完全不了解如何处理映射器。我不知道它应该如何工作,老实说,我还没有找到一个好的例子来说明如何做这样的事情。因此,任何帮助将不胜感激。     
已邀请:
        我对您的建议是看一下Doctrine,它是一个对象关系映射器,可以与ZF很好地集成在一起。我个人使用版本1.2和ZF 1.11.4都没有问题。 这里有一个不错的屏幕,解释了如何将教义与ZF互通。教义学起来可能有点麻烦,但是一旦您理解了它,从长远来看将为您节省时间。 Doctrine还附带一个命令行脚本,该脚本可用于将工程数据库反向转换为Doctrine类。如果您在数据库上设置了关系,则Doctrine也可以选择该关系。 我还听说ZF 2将包含Doctrine 2.0。 我用来创建Doctrine类的方法是首先在appication.ini文件中设置主义,然后将这些行添加到生产标头下的某处。
[production]

//...

; Doctrine settings
pluginpaths.Freedom_Zend_Application_Resource = \"Freedom/Zend/Application/Resource\"
resources.doctrine.connection_string = \"mysql://username:password@localhost/database_name\"
resources.doctrine.compiled = false ; use compiled version of Doctrine
resources.doctrine.cache = false ; use query cache

; Information required for models generator
resources.doctrine.models_path = APPLICATION_PATH \"/modules/default/models/Doctrine\"
resources.doctrine.module_directories[] = APPLICATION_PATH \"/modules/default/models/Doctrine/base\"  
resources.doctrine.module_directories[] = APPLICATION_PATH \"/modules/default/models/Doctrine\"

; Generator settings
resources.doctrine.generate_models_options.phpDocPackage = Your App Name
resources.doctrine.generate_models_options.phpDocSubpackage = Doctrine
resources.doctrine.generate_models_options.phpDocName = Your Company Name
resources.doctrine.generate_models_options.phpDocEmail = your@email.address
resources.doctrine.generate_models_options.pearStyle = true
resources.doctrine.generate_models_options.generateTableClasses = true
resources.doctrine.generate_models_options.generateBaseClasses = true
resources.doctrine.generate_models_options.classPrefix = \"Model_Doctrine_\"
resources.doctrine.generate_models_options.baseClassPrefix = \"Base_\"
resources.doctrine.generate_models_options.baseClassesDirectory =
resources.doctrine.generate_models_options.classPrefixFiles = false
resources.doctrine.generate_models_options.generateAccessors = false
//...
您会在顶部看到一条线
pluginpaths.Freedom_Zend_Application_Resource
自由是我库中的通用命名空间(请参见下面的文件夹树)。我在这里有一个Zend文件夹,可以在其中放置我的额外ZF代码,其中包括在需要时覆盖现有ZF函数的功能。自由是我的公司名称,您的显然会有所不同。此行需要更改为您公司的名称,例如
pluginpaths.Yourcompany_Zend_Application_Resource = \"Yourcompany/Zend/Application/Resource\"
下一行是放置数据库连接设置ѭ5的位置 接下来的三行:
resources.doctrine.models_path = APPLICATION_PATH \"/modules/default/models/Doctrine\"
resources.doctrine.module_directories[] = APPLICATION_PATH \"/modules/default/models/Doctrine/Base\"  
resources.doctrine.module_directories[] = APPLICATION_PATH \"/modules/default/models/Doctrine\"
告诉Doctrine在何处放置生成的类,因为我使用的是模块化设置,它们分别进入我的
application/modules/default/models/Doctrine
application/modules/default/models/Doctrine/Base
(请参阅下面的文件夹树)。 还有其他一些行需要更改,这些行不言而喻。 您还需要在application.ini的开发标题下添加以下行。
[development : production]

//...

; phpSettings
resources.doctrine.compiled = false ; use compiled version of Doctrine
resources.doctrine.cache = false ; use query cache

//...
您还将需要资源文件Doctrine.php。该文件的位置取决于您的设置,我的如下。
-Application
  -configs
  -layouts
  -modules
    -default // ZF default controller
      -controllers
      -models
        -Doctrine // folder for you generated classes
          -Base // Do not change the files in this folder
    -views
      -scripts
-Library
  -Doctrine // the doctrine application as downloaded
   doctrine-cli.php // you will need to create this (see below)
   Doctrine.php // come with the doctrine application
  -Freedom // my generic namespace, shared across multiple apps
    -Zend // place to overide/add ZF classes
      -Application
        -Resource
          *Docrine.php // the file below
  -Zend // ZF 1.11.4 (yours may differ)
  -ZendX // ZF extras
* Doctrine.php文件如下(显然忽略* !!)
/**
* Doctrine application resource
*
* @author Juozas Kaziukenas (juozas@juokaz.com)
*/
class Freedom_Zend_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
{
    /**
* Initialize
*/
    public function init()
    {
        $doctrineConfig = $this->getOptions();

        if (isset($doctrineConfig[\'compiled\']) && $doctrineConfig[\'compiled\'] == true &&
            file_exists(APPLICATION_PATH . \'/../library/Doctrine.compiled.php\'))
        {
            require_once \'Doctrine.compiled.php\';
        }
        else
        {
            require_once \'Doctrine.php\';
        }
        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->pushAutoloader(array(\'Doctrine_Core\', \'autoload\'), \'Doctrine\');

        $manager = Doctrine_Manager::getInstance();

        // set models to be autoloaded and not included (Doctrine_Core::MODEL_LOADING_AGGRESSIVE)
        $manager->setAttribute(
            Doctrine_Core::ATTR_MODEL_LOADING,
            Doctrine_Core::MODEL_LOADING_CONSERVATIVE
        );

        // enable ModelTable classes to be loaded automatically
        $manager->setAttribute(
            Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES,
            true
        );

        // enable validation on save()
        $manager->setAttribute(
            Doctrine_Core::ATTR_VALIDATE,
            Doctrine_Core::VALIDATE_ALL
        );

        // enable accessor override
        $manager->setAttribute(
            Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE,
            true
        );

        // enable sql callbacks to make SoftDelete and other behaviours work transparently
        $manager->setAttribute(
            Doctrine_Core::ATTR_USE_DQL_CALLBACKS,
            true
        );

        // enable automatic queries resource freeing
        $manager->setAttribute(
            Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS,
            true
        );

        // connect to database
        $manager->openConnection($doctrineConfig[\'connection_string\']);

        // set to utf8
        $manager->connection()->setCharset(\'utf8\');

        if (isset($doctrineConfig[\'cache\']) && $doctrineConfig[\'cache\'] == true)
        {
            $cacheDriver = new Doctrine_Cache_Apc();

            $manager->setAttribute(
                Doctrine_Core::ATTR_QUERY_CACHE,
                $cacheDriver
            );
        }

        return $manager;
    }
}
接下来,您将需要创建doctrine-cli.php文件来引导您的应用程序,该文件应位于您的库文件的根目录中(请参见上面的树),我的方法如下。
/**
 * Doctrine CLI script
 *
 * @author Juozas Kaziukenas (juozas@juokaz.com)
 */

define(\'APPLICATION_ENV\', \'development\');
define(\'APPLICATION_PATH\', realpath(dirname(__FILE__) . \'/../../your/application\'));

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . \'/../library\'),
    \'./\',
    get_include_path(),
)));

require_once \'Zend/Application.php\';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . \'/configs/application.ini\'
);

$application->getBootstrap()
        ->bootstrap(\'doctrine\')
        ->bootstrap(\'autoload\');

// set aggressive loading to make sure migrations are working
Doctrine_Manager::getInstance()->setAttribute(
    Doctrine::ATTR_MODEL_LOADING,
    Doctrine_Core::MODEL_LOADING_AGGRESSIVE
);

$options = $application->getBootstrap()->getOptions();

$cli = new Doctrine_Cli($options[\'resources\'][\'doctrine\']);

$cli->run($_SERVER[\'argv\']);
您需要更改的唯一一行是
define(\'APPLICATION_PATH\', realpath(dirname(__FILE__) . \'/../../your/application\'));
因此它指向应用程序的根文件夹,类似于公用文件夹中index.php文件中的行。 现在希望您已经准备好生成数据库类文件。转到终端 窗口并输入以下内容。
cd /home/path/to/library
php doctrine-cli.php generate-models-db
如果一切顺利,文件夹
application/modules/default/models/Doctrine
application/modules/default/models/Doctrine/Base
应该包含数据库的类。正如我上面提到的,不要更改Base文件夹中的文件,可以使用父Doctrine文件夹中的类进行更改。您还将注意到,Doctrine文件夹中的每个数据库表都有两个类,其中一个带有Table后缀。在这里,我倾向于放置我的DQL代码,以使其远离我的模型/控制器。其他类可用于更改基本文件夹中的类,也可以在此处添加挂钩和侦听器以添加表特定的代码,例如添加密码加密或预设日期等。 我希望我已经足够清楚地解释了这一点,因为这是工作的重中之重,但这就是我的工作方式。 我希望这有帮助。     

要回复问题请先登录注册