ZF-2577: _dependentTables and _referenceMap should have class parameter

Issue Type: Improvement Created: 2008-02-05T12:54:59.000+0000 Last Updated: 2009-08-06T10:50:29.000+0000 Status: Resolved Fix version(s): - 1.9.0 (31/Jul/09)

Reporter: Teemu Välimäki (cred) Assignee: Ralph Schindler (ralph) Tags: - Zend_Db_Table

Related issues: - ZF-2724

Attachments: - 1.5.1.patch


I tried to follow Coding Standard and Naming Conventions with my database models. But it all breaks when I try $paper->findPaperWriter() or $paperWriter->findParentPaper().

CREATE TABLE paper ... CREATE TABLE paper_writer ( paper_id... );

So I'd like to have

Database/Paper.php Database/Paper/Writer.php

class Database_Paper class Database_Paper_Writer

All of this would work, if I would call $paper->findDatabasePaperWriter() or $paperWriter->findParentDatabasePaperWriter(), but as you can see this is not a very good solution. I found two solutions.

  1. "class" parameter in _dependentTables and _referenceMap, that would tell ZF to look for certain class instead of trying magically resolve it. Zend/Db/Table/Row/Abstarct.php findDependentRowset() and findParentRow() should check for if there parameter has been set and use that instead of the one received as $dependentTable or $parentTable parameter.
  2. have addHelperPath() similar to Zend_View View Helper


Posted by Teemu Välimäki (cred) on 2008-02-05T13:14:34.000+0000

Quick test with Zend/Db/Table/Row/Abstract.php findDependentRowset() seems to work just fine!

<pre class="highlight">
        $dependapleTables = $this->_getTable()->getDependentTables();
        if (is_array($dependapleTables[$dependentTable])) {

            $dependentTable = new $dependapleTables[$dependentTable]['class'](array('db' => $db));
        } elseif (is_string($dependentTable)) {

Posted by Teemu Välimäki (cred) on 2008-02-05T13:15:36.000+0000

Apparently I don't know how to use comment markup on here, sorry about that.

Posted by Teemu Välimäki (cred) on 2008-02-06T06:08:46.000+0000

After using my fix for a while I noticed some problems with Parent magic methods. I noticed, that there's a lot of code that would require if clauses. So instead of option 1. I presented earlier I'm now leaning towards option 2. of using addDatabasePath(). Option 2 would also be backwards compatible and would ease working.

Posted by Teemu Välimäki (cred) on 2008-02-06T13:51:47.000+0000

After some copy paste from Zend_View I have a working Zend_Db_Table_Abstract and Zend_Db_Table_Row_Abstract, that utilizes addDatabasePath() concept and this time it even seems to work without a problem.

I'll generate a patch of it tomorrow after cleanup.

Posted by Teemu Välimäki (cred) on 2008-03-17T13:31:04.000+0000

This patch modifies Db/Table/Abstract.php and Db/Table/Row/Abstract.php adding the possibility to have prefixes for database files. I have database tables on each file so hierarchially so, that it's 1:1 to the datbase.

Example of Customer table: In bootstrap Zend_Db_Table_Abstract::addRelationshipPath('../library/Site/Db/Table', 'Site_Db_Table');

../library/Site/Db/Table/Customer.php class Site_Db_Table_Customer{}

Posted by Teemu Välimäki (cred) on 2008-03-17T15:48:12.000+0000

This is a proper patch. The previous contained hardcoding from my codebase.

Perhaps the first str_replace('' could be replaced with some ZF core function?

Posted by Teemu Välimäki (cred) on 2008-03-20T06:53:29.000+0000

The _addPath had a minor defunc, when it didn't check if the path already exists.

Fixed function

<pre class="highlight">
    private function _addPath($path, $prefix = null)
        foreach ((array) $path as $dir) {
            // attempt to strip any possible separator and
            // append the system directory separator
            $dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $dir);
            $dir = rtrim($dir, DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR)
                 . DIRECTORY_SEPARATOR;
            $params = array('prefix' => $prefix, 'dir' => $dir);

            if (!in_array($params, self::$_path)){
                // add as array with prefix and dir keys
                array_unshift(self::$_path, $params);

Posted by Teemu Välimäki (cred) on 2008-03-20T08:20:41.000+0000

I think this is change is required to maintain compability with those who wont be using RelationshipPath. The main point is the code inside else.

<pre class="highlight">
    protected function _prepareReference(Zend_Db_Table_Abstract $dependentTable, Zend_Db_Table_Abstract $parentTable, $ruleKey)
        if (count($parentTable->getRelationshipPath() > 0) {
            foreach ($parentTable->getRelationshipPath() as $relationshipPath) {
                $parentTable = str_replace('_', '', str_replace($relationshipPath, '', get_class($parentTable)));
        } else {
            $parentTable = get_class($parentTable);

Posted by Wil Sinclair (wil) on 2008-03-25T20:43:52.000+0000

Please categorize/fix as needed.

Posted by Teemu Välimäki (cred) on 2008-03-27T11:31:55.000+0000

This latest patch contains all the fixes applied against trunk.

Posted by Teemu Välimäki (cred) on 2008-03-27T11:55:28.000+0000

And of course I managed to screw newest patch up a bit. Table/Row/Abstract.php line 776 is missing closing )

Otherwise it's good to go

Posted by Teemu Välimäki (cred) on 2008-04-01T14:25:16.000+0000

My patches remove @ in front of Zend_Loader and thus, should to some extent solve the issue.

Posted by Wil Sinclair (wil) on 2008-12-04T13:17:30.000+0000

Reassigning to Ralph since he's the new maintainer of Zend_Db

Posted by Ralph Schindler (ralph) on 2009-01-10T11:09:06.000+0000

Will evaluate within 2 weeks

Posted by Ralph Schindler (ralph) on 2009-08-06T10:50:28.000+0000

Marking this as 'wont fix'. There are a couple of new components in place that would better facilitate what you are asking for. First is the module autoloader in Zend_Application. You can read about the Zend_Application_Module_Autoloader here… and here

As for the magic finder __call() method, I would tend to stay away from it: findParentClassNameByRule() in favor of the actual calls themselves: findParentRow($class ...) and findDependentRowset($class, ...)

Have a look at the new Zend_Application functionailty, i think it solves what you are looking for.


Have you found an issue?

See the Overview section for more details.


© 2006-2020 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.