Php 使用Zend_Db_适配器创建数据库

Php 使用Zend_Db_适配器创建数据库,php,zend-framework,zend-db,zend-db-table,Php,Zend Framework,Zend Db,Zend Db Table,我正在为应用程序的一部分而挣扎,我正在使用Zend_Db_Adapter在mysql中为每个客户端注册创建一个数据库模式 我有一个包含所有DDL的数据库脚本来创建表,然后读取文件的内容,然后使用Zend_Db_适配器执行它。在创建数据库之后,我想使用相同的适配器用一些数据填充它。但是,这始终会失败,并出现以下错误: 2011-08-10T14:15:16+01:00调试(7):SQLSTATE[42S02]:找不到基表或视图:1146表“client_3.users”不存在 有人能看到我的代码哪

我正在为应用程序的一部分而挣扎,我正在使用Zend_Db_Adapter在mysql中为每个客户端注册创建一个数据库模式

我有一个包含所有DDL的数据库脚本来创建表,然后读取文件的内容,然后使用Zend_Db_适配器执行它。在创建数据库之后,我想使用相同的适配器用一些数据填充它。但是,这始终会失败,并出现以下错误:

2011-08-10T14:15:16+01:00调试(7):SQLSTATE[42S02]:找不到基表或视图:1146表“client_3.users”不存在

有人能看到我的代码哪里有点错误吗?我已经让MySQL登录以查看任何错误,并且似乎没有任何错误,而且DDL脚本在手动运行时工作正常

$databaseName = 'client_' . $clientId;
$createDbStr = "DROP DATABASE IF EXISTS ".$databaseName."; CREATE DATABASE " . $databaseName.";";

            $dbAdapter->getConnection()->exec($createDbStr);

            //we now create the database adapter for the new database on the server
            //as we now populate the user data in the databaes.
            $newDbAdapter = Zend_Db::factory('Pdo_Mysql', array(
                'host' => 'localhost',
                'username' => 'appdbuser',
                'password' => 'appdbpassword',
                'dbname' => $databaseName
            ));


            //we now load the SQL Script that is used to create the database schema.
            $sqlScriptPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'datamodel' . DIRECTORY_SEPARATOR . 'client_schema.sql';
            $fileUtils = new App_FileSystem_FileUtility();
            $sqlScript = "USE ".$databaseName.";";
            $sqlScript .= $fileUtils->getFileContents($sqlScriptPath);

            //debug sql script;
            App_Application_Log::getInstance()->log($sqlScript, Zend_Log::DEBUG);

            //we now create the schema
            $newDbAdapter->getConnection()->exec($sqlScript);


            $usersTable = new Application_Model_UsersTable($newDbAdapter);
            $newUserTableData = array(
                'username' => 'Administrator',
                'fullname' => $contactName',
                'email' => $email,
                'uuid' => App_Security_Uuid::getInstance()->getUuid(),
                'create_date' => new Zend_Db_Expr('now()'),                
            );

            $userId = $usersTable->insert($newUserTableData); //errors here
在我开始用数据填充DDL脚本之前,是否需要从运行DDL脚本开始执行一个步骤

谢谢


Grant

我不认为
$dbAdapter->getConnection()->exec($createDbStr)
将像您一样运行两条SQL语句。将每个语句拆分为自己的语句(如果存在
drop table
create table

我不认为
$dbAdapter->getConnection()->exec($createDbStr)
将像您一样运行两条SQL语句。将每个表拆分为各自的语句(如果存在
下拉表
创建表

我现在有了解决此问题的方法,希望能帮助其他人

我所做的是确保create_schema.sql脚本在DDL中包含数据库名称

e、 g

然后,在我的PHP代码中,使用数据库名的str_replace函数和客户机数据库名进行了find replace

$sqlScriptPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'datamodel' . DIRECTORY_SEPARATOR . 'client_schema.sql';
            $fileUtils = new App_FileSystem_FileUtility();            
            $sqlScript = str_replace('dbname', $databaseName, $fileUtils->getFileContents($sqlScriptPath));

            //debug sql script;
            App_Application_Log::getInstance()->log($sqlScript, Zend_Log::DEBUG);

            //we now create the schema
            $newDbAdapter->getConnection()->exec($sqlScript);
然后,它创建了SQL脚本中描述的所有表和索引


希望这有帮助

我现在有了一个解决这个问题的办法,希望能帮助别人

我所做的是确保create_schema.sql脚本在DDL中包含数据库名称

e、 g

然后,在我的PHP代码中,使用数据库名的str_replace函数和客户机数据库名进行了find replace

$sqlScriptPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'datamodel' . DIRECTORY_SEPARATOR . 'client_schema.sql';
            $fileUtils = new App_FileSystem_FileUtility();            
            $sqlScript = str_replace('dbname', $databaseName, $fileUtils->getFileContents($sqlScriptPath));

            //debug sql script;
            App_Application_Log::getInstance()->log($sqlScript, Zend_Log::DEBUG);

            //we now create the schema
            $newDbAdapter->getConnection()->exec($sqlScript);
然后,它创建了SQL脚本中描述的所有表和索引


希望这有帮助

我会尝试向sqlScript添加一个
createtable
命令,例如:
$sqlScript=“USE”。$databaseName。“createtabletest(`id`int);”尝试缩小问题范围。我想
'fullname'=>$contactName'末尾的备用
已经不知何故进入了您的帖子,但不在您的代码中?假设它在那里甚至不会运行,只是检查一下而已。我已经做了你建议的事情,没有创建表:(“'fullname'=>$contactName'结尾的'是一个post输入错误。代码工作正常,执行良好;)好的,让我们试试:$newDbAdapter->getConnection()->exec(“创建表”$databaseName.)。测试(`id`int);”;
在您执行创建数据库之后。这很有效……我已经知道我现在需要做什么。ThanksI将尝试向您的sqlScript添加
create TABLE
命令,例如:
$sqlScript=“USE”。$databaseName。“创建表测试(`id`int);”试图缩小问题的范围。我假设
'fullname'=>$contactName'结尾的备用
不知怎么地进入了你的帖子,但不在你的代码中?如果它在那里,它可能不会运行,只是检查一下。我已经按照你的建议做了同样的问题,没有创建表:(.fullname结尾的“=>$contactName”是一个post输入错误。代码运行良好,执行良好;)好的,让我们试试:$newDbAdapter->getConnection()->exec(“创建表”。$databaseName。”。test(`id`int);”
在执行创建数据库之后。这很有效……我知道我现在需要做什么。感谢创建数据库很好,主要问题是创建数据库表,这似乎有点奇怪。创建数据库很好,主要问题是创建数据库表,这似乎有点奇怪。