0
votes

I am trying to build a Registration and login process how ever I am new to Zend I can't instantiate a model class: application/models/users.php class Users extends Zend_Db_Table_Abstract {

protected $_name = 'users';

}

in my bootstrap I performed autoloading this way

application/bootstrap

protected function _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;
}

on my controller I have this

public function indexAction() {

        $frmuser = new Application_Form_Contact;






    $users = new Application_Model_Users;





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

        // action body



    }

I have a form that works fine in application\forms\contact.php

class Application_Form_Contact extends 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;


}   

}

if I comment out the $users = new Application_Model_Users the form displays well but if uncommented, pointing to the url in the internet explorer browser will display the code like this:

class Users extends Zend_Db_Table_Abstract { protected $_name = 'users'; }

1
I just followed your steps Indrasinh Bihola as I always did before but I got a http error 500 on my browser when I called the url of the controller that instantiates the class. - fexio

1 Answers

0
votes

The easiest way to define a model is use zftool.

or you can make changes in your application like this to make your model work:

application\models\DbTable\users.php

class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract {

    protected $_name = 'users';

}

In your Controller now you are able to access it like this:

$users = new Application_Model_DbTable_Users();

Further details are provided in zend framework documentation:

Read it carefully to find out perfect way to implement it:

Zend Framework 1.12 Documentation