Archive

Archive for July, 2012

MySQL Temporary Tables and CakePHP 1.3

July 24th, 2012 2 comments

So, if you create a MySQL temporary table in CakePHP, when you try to create a model for that table, it dies, because mysql “Show Tables” doesn’t show temporary tables.  To get around this, implement the setSource function in your model..  i.e.

class WholesalerLeadsDataTemp extends AppModel{

	var $name = 'WholesalerLeadsDataTemp';
    function setSource($tableName) {
        $this->setDataSource($this->useDbConfig);
        $db =& ConnectionManager::getDataSource($this->useDbConfig);
        $db->cacheSources = ($this->cacheSources && $db->cacheSources);

        //MySQL Doesn't return temporary tables in the list of tables, so we have to disable this check...  (yay...)...
//        if ($db->isInterfaceSupported('listSources')) {
//            $sources = $db->listSources();
//            if (is_array($sources) && !in_array(strtolower($this->tablePrefix . $tableName), array_map('strtolower', $sources))) {
//                return $this->cakeError('missingTable', array(array(
//                    'className' => $this->alias,
//                    'table' => $this->tablePrefix . $tableName,
//                    'code' => 500
//                )));
//            }
//            $this->_schema = null;
//        }
        $this->table = $this->useTable = $tableName;
        $this->tableToModel[$this->table] = $this->alias;
        $this->schema();
    }
}
Categories: Uncategorized Tags: