我如何让 zend 识别 Propel ORM 的路径

发布于 2024-10-26 01:15:31 字数 1938 浏览 3 评论 0原文

我试图让 Propel 在我的 Zend 应用程序中工作,似乎我可以加载 Propel 库(从库/propel),但是当调用时我得到异常:“数据源的运行时配置文件中没有连接信息[默认]'(当我尝试建立连接时:'Propel::getConnection')。我的数据库甚至没有被命名为“默认”。我在另一个 SO 问题/答案的 bootstrap.php 中有这个:

           require_once 'propel/Propel.php';
       Propel::setConfiguration($this->getOptions('propel/MyPropel-conf.php'));
       Propel::initialize();

       // so we can get the connection from the registry easily
       return Propel::getConnection();

我希望 Propel 配置(以及类映射配置)位于“/application/configs”中(副本现在也在那里),但我想如果我可以让 Propel 从库/propel 加载,然后也许将我的“conf”文件移到那里,我也可以让它们加载。看来,如果我通过手动加载参数来“强制”配置,或者如果我似乎将其放在临时的“正确”位置(或使用绝对路径),那么我得到的异常是这样的: '无法打开 PDO 连接 [包装:SQLSTATE[28000] [1045] 用户'www-data'@'localhost' 的访问被拒绝 好像 Propel 没有关注我的配置。 我的配置如下所示;从 xml 转换:

    $conf = array (
  'datasources' => 
  array (
    'unmActTestDB' => 
    array (
      'adapter' => 'mysql',
      'connection' => 
      array (
        'dsn' => 'mysql://root:PASSWORD@localhost/unmActTestDB',
      ),
    ),
    'default' => 'unmActTestDB',
  ),
  'log' => 
  array (
    'ident' => 'propel-act',
    'level' => '7',
  ),
  'generator_version' => '1.5.6',
);
$conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-unmActTestDB-conf.php');
return $conf;

如果有帮助,我仍然在 application.ini 文件中加载 Zend PDO DB 适配器,这会导致冲突吗?有没有标准方法可以让 Propel 与 Zend 配合使用?或者有人能看到我做错了什么吗?

我在这里看过几篇关于 SO 的帖子,还有一些受欢迎的帖子,比如这个 将 Propel 与 Zend Framework 合并的冒险 以及 Zend 开发区的这篇文章 将 Propel 与 Zend Framework 集成,其中包括 Propel 文档。他们很有帮助,但我真的很挣扎。提前致谢!我当前的 Zend 目录结构如下所示(两个 propel conf 也在library/propel 文件夹中:

Directory Structure

I am trying to get Propel to work in my Zend app, it seems that I can get the Propel library to load (from library/propel) but when called I get the exception: 'No connection information in your runtime configuration file for datasource [default]' (when I try to make a connection: 'Propel::getConnection'). My Db is not even named 'default'. I have this in my bootstrap.php from another SO question/answer:

           require_once 'propel/Propel.php';
       Propel::setConfiguration($this->getOptions('propel/MyPropel-conf.php'));
       Propel::initialize();

       // so we can get the connection from the registry easily
       return Propel::getConnection();

I want the Propel configs (classmap conf as well) to be in the '/application/configs' (copies are there too right now), but I thought If I can get Propel to load from library/propel, then maybe moving my 'conf' files there, I may get them to load too. It seems that if I 'force' the config, by manually loading the params, or if I seem to get it in a temporary 'right' location (or use an absolute path), the exception I then get is this:
'Unable to open PDO connection [wrapped: SQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost'
As if Propel is not paying any attention to my configs.
My config looks like this; converted from the xml:

    $conf = array (
  'datasources' => 
  array (
    'unmActTestDB' => 
    array (
      'adapter' => 'mysql',
      'connection' => 
      array (
        'dsn' => 'mysql://root:PASSWORD@localhost/unmActTestDB',
      ),
    ),
    'default' => 'unmActTestDB',
  ),
  'log' => 
  array (
    'ident' => 'propel-act',
    'level' => '7',
  ),
  'generator_version' => '1.5.6',
);
$conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-unmActTestDB-conf.php');
return $conf;

If it helps, I still have the Zend PDO DB adapter loading in the application.ini file too, would that cause a clash?. Is there a standard way to get Propel to work with Zend? Or can anyone see what I'm doing wrong?

I have been to several posts here on SO, and a couple popular posts like this one The Adventures Of Merging Propel With Zend Framework and this one at Zend's dev zone Integrating Propel with the Zend Framework, among others including the Propel Docs. They have been helpful, but I am really struggling with this. Thanks in advance! My current Zend Directory structure looks like this (w/ the two propel confs also in the library/propel folder:

Directory Structure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-11-02 01:15:31

我最终做的是:

我很好地掌握了如何使用 Propel 从“反向”生成模型。它(像以前一样)创建了一个“schema.xml”文件供我用来构建模型。

另外,我的“runtime.xml”文件不符合我的需求,在构建之后,由于遗漏了一些标签,我没有连接到正确的数据库,但总的来说,它非常简单:

    <?xml version="1.0" encoding="UTF-8"?>
<config>
 <log>
  <ident>unmActTestDB</ident>
  <level>7</level>
 </log>
 <propel>
  <datasources default="unmActTestDB">  
   <datasource id="unmActTestDB">
    <adapter>mysql</adapter>    
    <connection>
    <dsn>mysql:host=localhost;dbname=unmActTestDB</dsn>
    <user>zend</user>
    <password>PASSWORD</password>
    <database>unmActTestDB</database>
    </connection>    
   </datasource>   
  </datasources> 
 </propel>
</config>

“dsn”标签必须位于上面的格式添加了“用户”、“密码”和“数据库”标签。这解决了我的数据库连接错误问题。正如上面(和这里)所引用的,抛出的异常是:“数据源的运行时配置文件中没有连接信息[默认]”

就我的模型的加载而言,我最终将它们放在我的“库”文件夹中,我我的应用程序中已经有该文件夹自动加载,而且它似乎是它们的好地方。

这是我的“更新的”目录结构的图像:My directory structure w/ Propel models

请注意添加了“unmActTestDB” ' 文件夹在我的目录中,这是我的 ORM 模型。
另一件需要注意的事情是,我将生成的“conf”文件放入“application/configs”文件夹中。在更正runtime.xml 文件并“重建”之后,这些现在是正确的。

作为旁注,我必须手动编辑我的“schema.xml”文件(几次:))...原始数据库对表使用复数名称,所以我编辑了所有“phpname”声明(实际上是属性,在声明标签上)是单数,所以我不会访问名为“Users”的对象...相反,我现在可以访问“User”对象。我保持表名称相同(表是复数,导入现有数据时不会有任何问题等)这是由我的另一个问题的答案建议的,请参阅此处 如何获取相关对象Propel ORM

我所做的另一个大编辑是为数据库中的许多 SQL 视图添加主键声明(同样是属性),我还在声明中添加了“只读”和“noSQL”属性,这样我就可以访问(通过 Propel 模型)表达我的观点。

而且,为了彻底,对于那些对此感兴趣的人来说,我的“bootstrap.php”文件中添加了一个内容,该文件为我执行“Propel Init”...

protected function _initPropel()
        {
            $this->_logger->info('Bootstrap ' . __METHOD__);

            require '../library/propel/Propel.php';
            Propel::init(APPLICATION_PATH . '/configs/unmActTestDB-conf.php');
            Propel::initialize();

            return Propel::getConnection();
        }

另一个注意事项:“$this->_logger->” ;info('Bootstrap'。方法);'调用我的“logging”方法,该方法只是告诉“firePHP”该方法已加载。 /configs/unmAtTestDB-conf.php'调用Propel生成的第二个'conf'文件...这是该文件的'更正'版本(即unmAtTestDB.conf文件),请注意'中的更改连接'数组。

<?php
// This file generated by Propel 1.5.6 convert-conf target
// from XML runtime conf file runtime-conf.xml
$conf = array (
  'datasources' => 
  array (
    'unmActTestDB' => 
    array (
      'adapter' => 'mysql',
      'connection' => 
      array (     
        'dsn' => 'mysql:host=localhost;dbname=unmActTestDB',
        'user' => 'zend',
        'password' => 'PASSWORD', 
        'database' => 'unmActTestDB', 
      ),
    ),
    'default' => 'unmActTestDB',
  ),
  'log' => 
  array (
    'ident' => 'unmActTestDB',
    'level' => '7',
  ),
  'generator_version' => '1.5.6',
);
$conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-unmActTestDB-conf.php');
return $conf;

我现在就出发了!这是一个很好的方法,否则我会为我的应用程序寻找和编写很多映射类。 Propel(当前)为此应用程序生成了 300 多个模型!再加上基础课程,这将花费我很长时间......

What I ended up doing is this:

I got a good grip on how to generate my models from a 'reverse' using Propel. It created (as before) a 'schema.xml' file for me to build my models with.

Also my 'runtime.xml' file was incorrect for my needs, after the build I was not connecting to the right database because of my omission of a few tags, overall it is quite simple though:

    <?xml version="1.0" encoding="UTF-8"?>
<config>
 <log>
  <ident>unmActTestDB</ident>
  <level>7</level>
 </log>
 <propel>
  <datasources default="unmActTestDB">  
   <datasource id="unmActTestDB">
    <adapter>mysql</adapter>    
    <connection>
    <dsn>mysql:host=localhost;dbname=unmActTestDB</dsn>
    <user>zend</user>
    <password>PASSWORD</password>
    <database>unmActTestDB</database>
    </connection>    
   </datasource>   
  </datasources> 
 </propel>
</config>

The 'dsn' tag has to be in the format above with the addition of the 'user', 'password', and the 'database' tags. This fixed my issue with the database connection errors. As quoted above (and here) the exception thrown was: 'No connection information in your runtime configuration file for datasource [default]'

As far as the loading of my models goes, I ended up putting them in my 'library' folder, I already have that folder autoloading in my app, plus it seemed like a good place form them.

here is an image of my 'updated' directory structure:My directory structure w/ Propel models

Note the addition of the 'unmActTestDB' folder in my directory, this is my ORM models.
Another thing to note is that I put my generated 'conf' files into my 'application/configs' folder. These are correct now, after the correction of the runtime.xml file and a 'rebuild'.

As a side note, I had to edit my 'schema.xml' file by hand (several times :) )...The original database used plural names for the tables, so I edited all the 'phpname' declarations (attributes actually, on the declaration tag) to be singular so I wouldn't access an object called 'Users'...instead I can now access a 'User' object. I kept the table names the same (tables are plural, and I won't have any issues importing the existing data, etc.) This was suggested by an answer to another one of my questions, see here How to get related object Propel ORM.

The other big edit I made was to add primary key declarations (again, attributes) for the many SQL views in the DB, also I added a 'readonly' and a 'noSQL' attributes to the declarations, this way I will have access (through the Propel models) to my views.

And, just to be thorough, and for those who are interested here is the addition to my 'bootstrap.php' file that does my 'Propel Init' for me...

protected function _initPropel()
        {
            $this->_logger->info('Bootstrap ' . __METHOD__);

            require '../library/propel/Propel.php';
            Propel::init(APPLICATION_PATH . '/configs/unmActTestDB-conf.php');
            Propel::initialize();

            return Propel::getConnection();
        }

Another NOTE: the '$this->_logger->info('Bootstrap ' . METHOD);' calls my 'logging' method that just tells 'firePHP' that this method has loaded. The /configs/unmActTestDB-conf.php' calls the second 'conf' file that Propel generates... and here is the 'corrected' version of that file (the unmActTestDB.conf file that is), Note the changes in the 'connection' array.

<?php
// This file generated by Propel 1.5.6 convert-conf target
// from XML runtime conf file runtime-conf.xml
$conf = array (
  'datasources' => 
  array (
    'unmActTestDB' => 
    array (
      'adapter' => 'mysql',
      'connection' => 
      array (     
        'dsn' => 'mysql:host=localhost;dbname=unmActTestDB',
        'user' => 'zend',
        'password' => 'PASSWORD', 
        'database' => 'unmActTestDB', 
      ),
    ),
    'default' => 'unmActTestDB',
  ),
  'log' => 
  array (
    'ident' => 'unmActTestDB',
    'level' => '7',
  ),
  'generator_version' => '1.5.6',
);
$conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-unmActTestDB-conf.php');
return $conf;

I am off and running now! This was a great way to go, otherwise I was looking and writing sooo many mapping classes for my app. Propel (currently) generates over 300 models for this application! Plus the base classes, it would take me forever...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文