Zend framework 如何在Zend框架中使用关系数据库?
我正在使用zend框架编写一个应用程序,但在让关系数据库模型正常工作时遇到了很多问题。我已经阅读了几次quickstart和文档,但我仍然不知道如何做到这一点。我张贴了一张不同表格之间关系的图片,以避免解释所有内容,因为英语不是我的第一语言,当我试图解释…复杂的事情时,我往往不清楚自己。 在名为planilla_users的表中,表press_releases、social_networking、blog_posts、rss_feed、directorios、users和articulos都设置为外键。我按照quickstart显示的方式对表格模型进行了编码:Zend framework 如何在Zend框架中使用关系数据库?,zend-framework,zend-db,zend-db-table,Zend Framework,Zend Db,Zend Db Table,我正在使用zend框架编写一个应用程序,但在让关系数据库模型正常工作时遇到了很多问题。我已经阅读了几次quickstart和文档,但我仍然不知道如何做到这一点。我张贴了一张不同表格之间关系的图片,以避免解释所有内容,因为英语不是我的第一语言,当我试图解释…复杂的事情时,我往往不清楚自己。 在名为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模型,其中包含所有用于存储/更新/检索/删除信息的setter和getter,…但是…我完全不知道如何使用映射器。我不知道它应该如何工作,老实说,我还没有找到一个关于如何做这样的事情的好例子。因此,任何帮助都将不胜感激。我给您的建议是看一看,它是一个对象关系映射器,可以很好地与ZF集成。我个人使用ZF 1.11.4的1.2版没有问题 有一个很好的屏幕演示,解释了如何将条令整合到ZF中。教义学起来有点像猪,但一旦你理解了它,从长远来看,它会节省你的时间。条令还附带了一个命令行脚本,可用于将数据库反向工程为条令类。如果您在数据库上设置了关系,那么条令也可以选择它 我还听说ZF 2将包含Doctrine 2.0 我用来创建条令类的方法是,首先在application.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\u Zend\u Application\u资源
Freedom是我的库中的通用名称空间(请参见下面的文件夹树)。我在这里有一个Zend文件夹,我可以在其中放置额外的ZF代码,这包括在需要时覆盖现有的ZF函数。自由是我的公司名称,你的公司名称显然不同。此行需要更改为您公司的名称,例如pluginpaths.Yourcompany\u Zend\u Application\u Resource=“Yourcompany/Zend/Application/Resource”
下一行是放置数据库连接设置的位置resources.doctor.connection\u string=”mysql://username:password@本地主机/数据库名称“
接下来的三行:
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"
告诉条令在哪里放置生成的类,因为我使用的是模块化设置,这些类分别进入我的<代码>应用程序/模块/默认/模型/条令<代码>和<代码>应用程序/模块/默认/模型/条令/基础代码>(见下面的文件夹树)
还有一些其他方面需要改变,这些应该是不言而喻的
您还需要在application.ini的development标题下添加以下行
[development : production]
//...
; phpSettings
resources.doctrine.compiled = false ; use compiled version of Doctrine
resources.doctrine.cache = false ; use query cache
//...
您还需要资源文件doctor.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 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']);
*doctor.php文件如下所示(显然忽略*!!)
接下来,您需要创建doctor-cli.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 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/doctor
和application/modules/default/models/doctor/Base
应该包含数据库的类。如上所述,如果不更改基本文件夹中的文件,您可以使用父文件夹中的类进行更改。您还将注意到,在“条令”文件夹中,每个数据库表有两个类,一个以表为后缀。在这里,我倾向于将DQL代码放在远离模型/控制器的地方。其他类可用于更改基本文件夹中的类,也可以在此处添加挂钩和侦听器,以添加特定于表的代码,例如添加密码加密或预设日期等
我希望我已经解释清楚了,因为这是一个猪的工作要开始工作,但这就是我的设置方式
我希望这能有所帮助。你看过这一章吗:?它来自参考指南,而不是快速入门指南,它应该给你所有的答案。@Raffael Luthiger是的,我看到了。但是,当我有8个不同的表参与多对一关系时,我不知道如何让映射器工作。例如,我如何获取所有()文件?这与您的?如果这个问题取代了它,也许可以删除这个问题?也许可以看看。大部分内容与您已经定义的内容类似,但在某种程度上它演示了如何实际使用已定义的关系。@David Weinraub,非常感谢这篇文章,我现在就来看看。我删除了另一个问题,所以谢谢你让我注意到Well@Garry,谢谢你的回答。我只听到过一些关于条令的好消息,但由于我目前正接近Zend框架本身,我担心选择条令对我来说可能有点苛刻……至少在这个阶段。不过,在我下定决心之前,我会先检查一下屏幕。这可能不像我现在想象的那么难。我现在正在学习zend cast关于ZF/条令集成的教程。我也在寻找关于如何为我现有的db表生成类的信息…(s)