0

My boss wants the application we are currently working on to be split across several schemata in the database, because he wants multiple applications -- some of which I have no control over -- to be able to access the data, following a naming convention like DeploymentPrefix_Category. For instance, there'd be a few schemata for production, Production_Foo, Production_Bar, and Production_Baz, and then the same for staging Staging_Foo, Staging_Bar, and Staging_Baz, and the same for development.

The problem is that while Zend_Db_Table lets me specify a schema, it doesn't seem to let me generate that schema on the fly, which I would need to do to put that prefix on the schema.

What's the best way to handle that?

1
  • Short answer: maybe this will help. Commented Sep 29, 2011 at 16:00

2 Answers 2

1

The issue of different configs for different environments is easily handled with Zend_Config. See the section on config in the quickstart:

http://framework.zend.com/manual/en/learning.quickstart.create-project.html

This allows you to specify different settings for each environment.

As for the schemas, I'm guessing you have some tables that live in Production_Foo and others that live in Production_Bar. Consider extending Zend_Db_Table for each of these schemas and pointing to the correct database at the time of construction.

Zend_Db_Table's constructor is defined as follows:

public function __construct($config = array(), $definition = null)
    { ... }

When we follow through to see where $definition leads it allows you to pass an array that is loaded into Zend_Db_Table_Definition. One of the options for this is the table name:

/**
 * @param string $tableName
 * @param array  $tableConfig
 * @return Zend_Db_Table_Definition
 */
public function setTableConfig($tableName, array $tableConfig)
{
    // @todo logic here
    $tableConfig[Zend_Db_Table::DEFINITION_CONFIG_NAME] = $tableName;
    $tableConfig[Zend_Db_Table::DEFINITION] = $this;

    if (!isset($tableConfig[Zend_Db_Table::NAME])) {
        $tableConfig[Zend_Db_Table::NAME] = $tableName;
    }

    $this->_tableConfigs[$tableName] = $tableConfig;
    return $this;
}

As for your schema, you just pass in different set of options for the db adapter that points to the correct one.

Sign up to request clarification or add additional context in comments.

4 Comments

We already have the multiple deployments setup using the default Zend_Application bits -- we don't have a way of injecting the prefixes into the schema references in the table classes though.
That is, different Zend_Db_Table classes have to use different schemata
Check the 2nd parameter in the constructor for Zend_Db_Table. You can pass a config object or array to the definition. One of the parameters you can set is the table name: public function setTableConfig($tableName, array $tableConfig)
I've added references to this above.
1

Well i'd argue that it's not "good" to have different table-names for different staging scenarios "Production_Foo" - "Staging_Foo" - "Testing_Foo".... just "Foo" is so much easier and more productive...

But anyways: Personally i use the Table-Data-Gateway (i guess that's what it's called) - using Zend_Db_Table_Abstract extensions, so i would do it like this:

class Application_Model_DbTable_Foo extends Zend_Db_Table_Abstract 
{
  public function __construct($config = array()) {
    $this->_name = Zend_Registry::get('config')->env_tbl_prefix.'Foo';
    parent::__construct($config);
  }
}

Obviously this requires you to have the config stored to the registry and inside the config to define the key "env_tbl_prefix" with your environment prefixes "Production_", "Staging_", "Testing_", etc...

Still, you're the developer, tell your boss to make life easier for all of you ^^ There's so many disadvantages using different table names depending on environment :\

6 Comments

I agree with @Sam. This is just a terrible idea. You never want to mix the data from different staging areas. At minimum, you should separate the staging areas with different database names, or better yet, different hosts.
You can't do that because class properties must be constant, that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
@drew010 ok, that i should have known by now... and i call myself good at what i do, sigh :P But if your class extends zend db table doing: public function __construct($config) { $this->_name = Zend_Registry::get('config')->env_tbl_prefix.'Foo'; parent::__construct($config); } should work, right?
thats okay, i have made that mistake a few times which is why i know it so well now. I don't see any problems with changing the name in the constructor like you do now, I think that is good.
@Sam: We aren't talking about different table names; we're talking about different schemata. You don't want your development and staging systems pointing at the production data -- a bug on your dev box shouldn't be able to blow away production data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.