1
votes

I am building a custom plugin, which consists of schema which I am trying to keep tightly coupled with the plugin. For example, I have a plugins/userPlugin/config/doctrine/schema.yml which references a 'store-rw-user' connection. My goal is to force the project's databases.yml to contain a connection for 'store-rw-user', this way the project will be responsible for setting up the separate user connection.

However, when I attempt to access plugin related code I keep getting: Doctrine_Manager_Exception Unknown connection: store-rw-user.

Here is the related snippet of the plugin schema file:

# plugins/userPlugin/config/doctrine/schema.yml
connection: store-rw-user

User:
  tableName: user
  actAs: [Timestampable]    

And here is the related snippet of the BaseUser.class.php (generated when doing a model build):

<?php
// lib/model/doctrine/userPlugin/base/BaseUser.class.php

// Connection Component Binding
Doctrine_Manager::getInstance()->bindComponent('User', 'store-rw-user');

....

And finally the related snippet of the project's databases.yml file:

# config/databases.yml
all:
  store-rw-user:
    class: sfDoctrineDatabase
    param:
    ....

On the outset, everything looks configured correctly. The plugin schema.yml resulted in the base class binding the correct database connection, which exists in the project's databases.yml file. However, I am running into the aforementioned issue. I'd like to know if there is another approach to this issue before I start trying to manipulate doctrine connection manager manually via the plugins initialize() method.

-- Update --

After further investigation, it appears that sfDatabaseManager is aware of the 'store-rw-user' connection, however, Doctrine_Manager is not. At first I thought this was maybe due to the order in which I added plugins within the main project config file, but it only affected whether sfDatabaseManager was connection aware, but for the sake of completeness, here is the snippet of the project config file:

// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
    ....

    public function setup()
    {
        ....

        $this->enablePlugins(
            array(
                'sfDoctrinePlugin',
                'userPlugin'
            )
        );
    }
}

And just in case it matters, here is the app.yml for the userPlugin:

# plugins/userPlugin/config/app.yml
# This entry was needed so build-model was executed, the plugin schema file would be pulled in for build as well
all:
  userPlugin:
    config_dir: %SF_PLUGINS_DIR%/userPlugin/config
    recursive: true

And here is the confusing snippet of code, where I have access to 'store-rw-user' connection via sfDatabaseManager but not Doctrine_Manager, which is a problem because the error is being thrown from Doctrine_Manager and not sfDatabaseManager:

// plugins/userPlugin/config/userPluginConfiguration.class.php
class userPluginConfiguration extends sfPluginConfiguration
{
    public function initialize()
    {
        parent::initialize();

        var_dump(Doctrine_Manager::getInstance()->getConnections());

        $config = ProjectConfiguration::getActive();

        $manager = new sfDatabaseManager($config);

        var_dump($manager->getDatabase('store-rw-user'));

        exit;
    }
}

And the results:

array (size=0)
  empty

object(sfDoctrineDatabase)[53]
  protected '_doctrineConnection' => 
    object(Doctrine_Connection_Mysql)[55]
      protected 'driverName' => string 'Mysql' (length=5)
      protected 'dbh' => null
      protected 'tables' => 
        array (size=0)
          empty
      protected '_name' => string 'store-rw-user' (length=13)
....

Not sure what's going on, tried looking at other plugins to see how they deal, and they don't have any database references that I could find.

1
Don't you have an indentation mistake in your databases.yml: store-rw-user is on the same level of class. Is it a typo in your question or also in your databases.yml?j0k
Good catch, it was only malformed in the question, databases.yml is spaced correctly.Mike Purcell

1 Answers

2
votes

After adding some debug code to select source files (Doctrine_Manager), I finally saw that in the above code snippet in the initialize() method, that by simply configuring sfDatabaseManager, doctrine then became aware of the connection. In short, my method ended up looking like this:

class userPluginConfiguration extends sfPluginConfiguration
{
    public function initialize()
    {
        parent::initialize();

        // Don't ask me why, without this the needed db connection never gets initialized and plugin craps out
        $manager = new sfDatabaseManager(ProjectConfiguration::getActive());
    }
}

And, magically, doctrine was now aware of all the connections in the project's databases.yml file. Seems weak to me that I have to explicitly make this call, but at least I can get on with some coding.