duanhuan2301 2011-08-10 13:22
浏览 54
已采纳

使用Zend_Db_Adapter创建数据库

I'm struggling with a part of my application where I am using Zend_Db_Adapter to create a database schema in mysql per client signup.

I have a database script that contains all the DDL to create the tables, I then read the contents of the file and then execute it using the Zend_Db_Adapter. Following the creation of the database I want to populate it with some data using the same adapter. However this always fails with an error such as:

2011-08-10T14:15:16+01:00 DEBUG (7): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'client_3.users' doesn't exist

Can someone see where in my code it's going slightly wrong? I've had the MySQL logging up to see any errors and there don't appear to be any, and the DDL script works fine when ran by hand.

$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

Is there a step that needs to be done from the running of the DDL script before I can start populating it with data?

Thanks,

Grant

  • 写回答

2条回答 默认 最新

  • douchong4730 2011-08-12 21:12
    关注

    I have a solution to this problem now which I hope will help others.

    What I did was ensure that the create_schema.sql script had the database names in the DDL

    e.g.

    CREATE TABLE `dbname`.`users`(
     userid INT,
     username VARCHAR(255)
    )
    

    I then, in my PHP code did a find replace of the using the str_replace function of the database name with with name of the clients database.

    $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);
    

    This then created all the tables and indexes as described in the SQL script.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭