Issues

ZF-36: Specify fetch mode for internal Zend_Db queries

Issue Type: Bug Created: 2006-06-19T23:03:24.000+0000 Last Updated: 2007-07-05T14:43:07.000+0000 Status: Resolved Fix version(s): - 0.8.0 (21/Feb/07)

Reporter: Zend Framework (zend_framework) Assignee: Bill Karwin (bkarwin) Tags: - Zend_Db

Related issues: - ZF-618

Attachments:

Description

http://framework.zend.com/developer/ticket/23

Zend_Db_Adapter_Pdo_Mysql::describeTable() assumes that an array is return from the query but this might not be the case if Zend_Db_Adapter::setFetchMode() has been called.

Example: Zend_Db_Adapter::setFetchMode(Zend_Db::FETCH_OBJ)

This is a major problem when using the the Zend_Db_Table since Zend_Db_Adapter_Pdo_Mysql::describeTable() is called in the Zend_Db_Table::_setup().

Suggestion: I would suggest implementing Zend_Db_Adapter::restoreFetchMode() so that components can use their preferred fetch mode without affecting each other.

If needed Zend_Db_Adapter::setFetchMode() could store an stack of previous fetch modes so that the users program won't be affected by components calling each other.

Note: This problem probably exists in all the Zend_Db_Addapter_* classes but has not been verified.

See "Problem Area" below in code from Zend_Db_Adapter_Pdo_Mysql:

<pre class="highlight">
        public function describeTable($table) { debug_print_backtrace(); die;

                $sql = "DESCRIBE $table"; 
                $result = $this->fetchAll($sql); 
                $descr = array(); 
                foreach ($result as $key => $val) {

                        $descr[$val['field']] = array( // <--Problem Area: Array Assumed

                                'name' => $val['field'], 
                                'type' => $val['type'],
                                'notnull' => (bool) ($val['null'] === ), // not null is empty, null is yes 
                                'default' => $val['default'],
                                'primary' => (strtolower($val['key']) == 'pri'),
                        );

                } 
                return $descr;
        }

Comments

Posted by Zend Framework (zend_framework) on 2006-06-19T23:03:42.000+0000

<pre class="highlight">

Possible solution:-

Existing code:-

    public function describeTable($table)
    {
        $sql = "DESCRIBE $table";
        $result = $this->fetchAll($sql);
        $descr = array();
        foreach ($result as $key => $val) {
            $descr[$val['field']] = array(
                'name'    => $val['field'],
                'type'    => $val['type'],
                'notnull' => (bool) ($val['null'] != 'YES'), // not null is NO or empty, null is YES

                'default' => $val['default'],
                'primary' => (strtolower($val['key']) == 'pri'),
            );
        }
        return $descr;
    }

Replace with:-

    public function describeTable($table)
    {
        $sql = "DESCRIBE $table";
        $result = $this->query($sql);
        $result = $result->fetchAll(PDO::FETCH_ASSOC);
        $descr = array();
        foreach ($result as $key => $val) {
            $descr[$val['field']] = array(
                'name'    => $val['field'],
                'type'    => $val['type'],
                'notnull' => (bool) ($val['null'] != 'YES'), // not null is NO or empty, null is YES

                'default' => $val['default'],
                'primary' => (strtolower($val['key']) == 'pri'),
            );
        }
        return $descr;
    }

Posted by Bill Karwin (bkarwin) on 2006-11-13T15:14:34.000+0000

Changing fix version to 0.8.0.

Posted by Bill Karwin (bkarwin) on 2006-12-29T14:42:29.000+0000

Changed summary.

The latter solution is best, to specify the fetch mode on a case-by-case basis. It should be applied throughout the Zend_Db classes, everywhere where internal queries depend on a certain fetch mode.

Posted by Bill Karwin (bkarwin) on 2007-01-24T21:09:32.000+0000

reformat to remove long line

Posted by Bill Karwin (bkarwin) on 2007-02-07T21:05:24.000+0000

Fixed in revision 3284.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

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

Contacts