2011-07-27 43 views
0

我的應用程序模型有問題。它應該在mysql數據庫中保存一個新用戶。Zend_Db_Abstract子類(模型)

應用/配置/的application.ini

[production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
includePaths.library = APPLICATION_PATH "/../library" 
bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 
bootstrap.class = "Bootstrap" 
appnamespace = "Application" 
resources.frontController.params.displayExceptions = 1 
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" 
resources.layout.layoutPath = APPLICATION_PATH "/layouts" 
resources.frontController.params.displayExceptions = 0 
resources.db.adapter = "pdo_mysql" 
resources.db.params.host = "localhost" 
resources.db.params.username = "root" 
resources.db.params.password = "pass" 
resources.db.params.dbname = "cms" 
resources.db.isDefaultTableAdapter = true 

[staging : production] 

[testing : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 

[development : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
resources.frontController.params.displayExceptions = 1 

應用/控制器/ MemberController.php:

public function newAction() 
{ 
    $frmNewMember = new Form_MemberRegisterationForm(); 
    $frmNewMember->setMethod('post'); 
    $frmNewMember->setAction('/member/new'); 
    if ($this->getRequest()->isPost()) 
    { 
     if ($frmNewMember->isValid($_POST)) 
     { 
      $memberModel = new Model_Members(); 
      // process the data 
      $result = true; 
      $result = $memberModel->createMember(
       $frmNewMember->getValue('name'), 
       $frmNewMember->getValue('lastName'), 
       $frmNewMember->getValue('uid'), 
       $frmNewMember->getValue('email'), 
       $frmNewMember->getValue('password')); 
      if ($result) 
      { 
       $this->_forward('confirm'); 
      } 
     } 
    } 
    $this->view->form = $frmNewMember; 
} 

應用/模型/ Members.php:

public function createMember($name, $lastName, $uid, $email, $password) 
{ 
    // Create row data 
    $row = $this->createRow(); 
    $row->name = $name; 
    $row->lastName = $lastName; 
    $row->uid = $uid; 
    $row->email = $email; 
    $row->pasword = md5($password); 
    // Save the row 
    $row->save(); 
    $id = $this->_db->lastInsertId(); 
    return $id; 
} 

當我點擊發送按鈕此錯誤發生:

Fatal error: Undefined class constant 'EXCEPTION_NO_ROUTE' 
in /var/www/project/application/controllers/ErrorController.php on line 11 

我知道問題是關於$row = $this->createRow();的代碼。因爲當我用return $true替換整個createMember方法的正文時,沒有錯誤。

UPDATE:

這是我在Bootsreap.php _initAutoload()。我應該添加一些配置數據庫?

protected function _initAutoload() 
{ 
    // Add autoloader empty namespace 
    $autoLoader = Zend_Loader_Autoloader::getInstance(); 
    $autoLoader->registerNamespace('CMS_'); 
    $resourceLoader = new Zend_Loader_Autoloader_Resource(
     array ('basePath' => APPLICATION_PATH, 'namespace' => '', 
      'resourceTypes' => array (
       'form' => array ('path' => 'forms/', 
        'namespace' => 'Form_'), 
       'model' => array ('path' => 'models/', 
        'namespace' => 'Model_')))); 
    // Return it so that it can be stored by the bootstrap 
    return $autoLoader; 
} 

月2日更新: 引發的異常是:

異常 'Zend_Db_Adapter_Exception' 有消息 'SQLSTATE [HY000] [2002]無法通過socket連接到本地MySQL服務器' 的/ tmp//usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Pdo/Abstract.php:138中的mysql.sock'(2)'堆棧跟蹤:#0/usr/local/zend/share/ZendFramework /library/Zend/Db/Adapter/Pdo/Mysql.php(96):Zend_Db_Adapter_Pdo_Abstract - > _ connect()#1 /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Abstract.php(444) ):Zend_Db_Adapter_Pdo_Mysql - > _ connect()#2/usr/local/zend/share/ZendFramework/libra ry/Zend/Db/Adapter/Pdo/Abstract.php(232):Zend_Db_Adapter_Abstract-> query('DESCRIBE cms ....',Array)#3/usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Pdo/Mysql.php(156):Zend_Db_Adapter_Pdo_Abstract-> query('DESCRIBE cms ....')#4/usr/local/zend/share/ZendFramework/library/Zend/Db/Table/Abstract。 php(727):Zend_Db_Adapter_Pdo_Mysql-> describeTable('members','cms')#5 /usr/local/zend/share/ZendFramework/library/Zend/Db/Table/Abstract.php(753):Zend_Db_Table_Abstract - > _ setupMetadata ()#6 /usr/local/zend/share/ZendFramework/library/Zend/Db/Table/Abstract.php(1297):Zend_Db_Table_Abstract - > _ getCols()#7/var/www/project/application/models /成員.php(33):Zend_Db_Table_Abstract-> createRow(Array,NULL,true)#8 /var/www/project/application/controllers/MemberController.php(32):Model_Members-> createMember('1','1', '1','soroush.rabiei @ ...',NULL)#9 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Action.php(512):MemberCo (#)/ 11/usr/local/zend/share/ZendFramework/library/Zend/Controller/Front.php(945):Zend_Controller_Dispatcher_Standard-> dispatch(Object(Zend_Controller_Request_Http),Object(Zend_Controller_Response_Http))#12/usr/local/zend/share/ZendFramework/library/Zend /Application/Bootstrap/Bootstrap.php(77):Zend_Controller_Front-> dispatch()#13 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(328):Zend_Application_Bootstrap_Bootstrap-> run()#14在/ var/WWW /工程/公共/指數。PHP(26):Zend_Application->的run()#{15}主要

+0

你在ErrorController使用上線11級有什麼描述?它是「Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_ROUTE」嗎?問題出在ErrorController中。解決這個問題,你會看到模型拋出的錯誤。 –

+0

好的,我這樣做了,現在結果是'發生了錯誤 應用程序錯誤' –

回答

1

異常消息是很清楚的:

exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)' in /usr/local/zend/share/ZendFramework/library/Zend/Db/Adapter/Pdo/Abstract.php:138 

這意味着您的數據庫驅動程序試圖通過位於插座連接到MySQL /tmp/mysql.sock在您的服務器上,但沒有找到它。

你有3種選擇:

  • 配置MySQL服務器(通過my.cnf中)把它在/ tmp目錄插座。應該仔細使用這個選項,因爲它會影響所有使用相同mysql實例的應用程序。
  • 找到mysql放入套接字的位置,然後編輯php.ini指向該套接字。這個選項也必須謹慎使用,因爲它對服務器上使用的每個php應用程序都有影響

例如: ;下在php.ini pdo_mysql.default_socket = /的mysql.sock

  • 的[PDO_MYSQL]部分配置應用程序通過套接字連接。這是一個PDO選項,它應該傳遞給Zend_Db_Adapter構造函數,如this page的示例#7中所述。

的選項名爲unix_socket並在PDO DSN php.net文檔頁面

+0

我向application.ini添加了'resources.db.params.unix_socket =「/ var/run/mysqld/mysqld.sock」 ''。謝謝 –