Zend_Db_Abstract子类(模型)

时间:2011-07-27 18:50:59

标签: php mysql zend-framework

我的应用程序模型有问题。它应该将新用户保存在mysql数据库中。

应用/配置/的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();代码有关。因为当我用createMember替换整个return $true方法的主体时,没有错误。

更新:

这是我在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;
}

第二次更新: 抛出的异常是:

异常'Zend_Db_Adapter_Exception',消息'SQLSTATE [HY000] [2002]无法通过套接字'/tmp/mysql.sock'(2)'连接到本地MySQL服务器/ usr / local / zend / share / ZendFramework /library/Zend/Db/Adapter/Pdo/Abstract.php:138堆栈追踪:#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 / library / 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 /表/文摘act.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/Members.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):MemberController-> newAction()#10 /usr/local/zend/share/ZendFramework/library/Zend/Controller/Dispatcher/Standard.php(288):Zend_Controller_Action-> dispatch('newAction ')#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_Contro ller_Front-> dispatch()#13 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(328):Zend_Application_Bootstrap_Bootstrap-> run()#14 / var / www / project / public / index .php(26):Zend_Application-> run()#15 {main}

1 个答案:

答案 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

这意味着您的数据库驱动程序尝试通过服务器上/tmp/mysql.sock中的套接字连接到mysql,但找不到它。

您有3个选项:

  • 配置你的mysql服务器(通过my.cnf)将它的套接字放在/ tmp中。应谨慎使用此选项,因为它对使用相同mysql实例的所有应用程序产生影响
  • 找到mysql放置套接字的位置,并编辑你的php.ini指向该套接字。此选项也必须小心使用,因为它对服务器上使用的每个php应用程序都有影响

示例:     ;在php.ini的[Pdo_mysql]部分下     pdo_mysql.default_socket = /的mysql.sock

  • 配置应用程序以通过套接字进行连接。这是一个PDO选项,它应该传递给Zend_Db_Adapter构造函数,如this page的示例#7中所述。

该选项名为unix_socket,在PDO DSN php.net doc page

中有描述