Issues

ZF2-66: Error in the class Zend\Db\Table\AbstractTable in the method getReference().

Description

Error in the class Zend\Db\Table\AbstractTable in the method getReference(). On line 468 there is no processing of the table class name (do not add a slash at the beginning), which is why there is a difference between "get_class($this)" and "$tableClassname" and resulting in the table class name is not in the "$_referenceMap" and throws exception: "Reference rule \" $ ruleKey \ "does not reference table $ tableClassname".

Solution: Make the processing of the variable $tableClassname at the beginning of the method.


    /**
     * @param string $tableClassname
     * @param string $ruleKey OPTIONAL
     * @return array
     * @throws \Zend\Db\Table\Exception
     */
    public function getReference($tableClassname, $ruleKey = null)
    {
        $tableClassname = '\\' . ltrim($tableClassname, '\\');
        $thisClass = get_class($this);
        if ($thisClass === 'Zend\Db\Table\Table') {
            $thisClass = $this->_definitionConfigName;
        }
        $refMap = $this->_getReferenceMapNormalized();
        if ($ruleKey !== null) {
            if (!isset($refMap[$ruleKey])) {
                throw new Exception("No reference rule \"$ruleKey\" from table $thisClass to table $tableClassname");
            }
            if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
                throw new Exception("Reference rule \"$ruleKey\" does not reference table $tableClassname");
            }
            return $refMap[$ruleKey];
        }
        foreach ($refMap as $reference) {
            if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
                return $reference;
            }
        }
        throw new Exception("No reference from table $thisClass to table $tableClassname");
    }

Comments

Solution:


    /**
     * @param string $tableClassname
     * @param string $ruleKey OPTIONAL
     * @return array
     * @throws \Zend\Db\Table\Exception
     */
    public function getReference($tableClassname, $ruleKey = null)
    {
        $tableClassname = '\\' . ltrim($tableClassname, '\\');
        $thisClass = get_class($this);
        if ($thisClass === 'Zend\Db\Table\Table') {
            $thisClass = $this->_definitionConfigName;
        }
        $refMap = $this->_getReferenceMapNormalized();
        if ($ruleKey !== null) {
            if (!isset($refMap[$ruleKey])) {
                throw new Exception("No reference rule \"$ruleKey\" from table $thisClass to table $tableClassname");
            }
            if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
                throw new Exception("Reference rule \"$ruleKey\" does not reference table $tableClassname");
            }
            return $refMap[$ruleKey];
        }
        foreach ($refMap as $reference) {
            if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
                return $reference;
            }
        }
        throw new Exception("No reference from table $thisClass to table $tableClassname");
    }

Add code tags.

I would not worry with fix of issues Zend\Db because this go be refactoring in version 2 still ;). http://framework.zend.com/wiki/display/…. https://github.com/ralphschindler/zf2-db.