Home > Uncategorized > MySQL Temporary Tables and CakePHP 1.3

MySQL Temporary Tables and CakePHP 1.3

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();
    }
}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Categories: Uncategorized Tags:
  1. Dipu
    December 5th, 2012 at 11:52 | #1

    Thanks a lot …it saved a lot of work ..

  2. December 5th, 2012 at 12:28 | #2

    @Dipu My pleasure. We’ve implemented this now in our system by creating a single “NoTableCheckModel” that has this method, and then anytime we create a temp table, we just extend that model.

  1. No trackbacks yet.