【问题标题】:I cannot instantiate a model in zend framwork 1.12我无法在 zend 框架 1.12 中实例化模型
【发布时间】:2014-11-19 20:22:31
【问题描述】:

我正在尝试构建一个注册和登录过程,但是我是 Zend 的新手,但我无法实例化模型类: 应用程序/模型/users.php 用户类扩展 Zend_Db_Table_Abstract {

protected $_name = 'users';

}

在我的引导程序中,我以这种方式执行自动加载

应用程序/引导程序

受保护的函数_initAutoload()

{
    //Add autoloader empty namespace
    require_once 'Zend/Loader/Autoloader.php';
    $autoLoader = Zend_Loader_Autoloader::getInstance();
    $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;
}

在我的控制器上我有这个

公共函数 indexAction() {

        $frmuser = new Application_Form_Contact;






    $users = new Application_Model_Users;





    $this->view->form = $frmuser;

        // action body



    }

我有一个可以在 application\forms\contact.php 中正常工作的表单

Application_Form_Contact 类扩展 Zend_Form

{

    public function init()


{




        $this->setMethod('post');



        /* Form Elements & Other Definitions Here ... */


    $this->addElement('text', 'username',array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter a Username:', 'validators' => array   

        (array('StringLength', false, array(2, 50)))));


    $this->addElement('text', 'firstname',array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter your Firstname:', 'validators' =>
        array(array('StringLength', false, array(2, 50)))));

    $this->addElement('text', lastname, array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter your Lastname:', 'validators' => array 

            (array('StringLength', false, array(2, 50)))));

    $this->addElement('text', 'email', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter your Email:', 'validators' => array 

            ('EmailAddress', array

('StringLength', false, array(2, 50)))));

    $this->addElement('text', 'emailagain', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Confirm your Email:', 'validators' =>    

        array('EmailAddress', array('StringLength', false, array(2, 50)))));

    $this->addElement('password', 'password', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Enter a password:', 'validators' =>    

        array(array('StringLength', false, array(2, 50)))));

    $this->addElement('password', 'passwordagain', array('filters' => array('StringTrim'), 'required' => true,'label' => 'Confirm your password:',      

        'validators'=> array(array('StringLength', false, array(2, 50)))));


    $this->addElement('submit', 'contact');
    $this->view->form = $form;


}   

}

如果我注释掉 $users = new Application_Model_Users 表单会显示良好,但如果未注释, 指向 Internet Explorer 浏览器中的 url 将显示如下代码:

用户类扩展 Zend_Db_Table_Abstract { protected $_name = 'users'; }

【问题讨论】:

  • 我只是像以前一样按照您的步骤 Indrasinh Bihola,但是当我调用实例化类的控制器的 url 时,我的浏览器上出现了 http 错误 500。

标签: zend-framework


【解决方案1】:

定义模型的最简单方法是使用 zftool

或者您可以像这样在您的应用程序中进行更改以使您的模型正常工作:

application\models\DbTable\users.php

class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract {

    protected $_name = 'users';

}

现在您可以在控制器中访问它:

$users = new Application_Model_DbTable_Users();

zend 框架文档中提供了更多详细信息:

仔细阅读,找出实现它的完美方法:

Zend Framework 1.12 Documentation

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多