0

I am new to zend frame work . I am trying to learn zf create db-table tblename,zf create model tblname,zf create model tblnameMapper.

add user.php (form)

<?php

class Application_Form_Adduser extends Zend_Form
{

    public function init()
    {
                $this->setMethod('post');


        $username = $this->createElement('text','username');
        $username->setLabel('Username:')
                ->setAttrib('size',10);
        $password=$this->createElement('text','password');
        $password->setLabel('Password')
                 ->setAttrib('size',10);

        $reg=$this->createElement('submit','submit');
        $reg->setLabel('save');             

        $this->addElements(array(
        $username,
        $password,
        $reg
        ));



        return $this;
    }

add user view:

<?php 
echo 'Add user';
echo "<br />" .$this->a;
echo $this->form;

?>

admin controller:

<?php

class AdminController extends Zend_Controller_Action

{

    public function adduserAction(){
           $form = new Application_Form_Auth();
        $this->view->form = $form;
        $this->view->assign('a','Entere new user:');
        if($this->getRequest()->isPost()){

            $formData  = $this->_request->getPost();            
            $uname=$formData['username'];
            $pass=$formData['password'];
            $msg=$uname." added to database successfully";

            $this->view->assign('a',$msg);
            $db= Zend_Db_Table_Abstract::getDefaultAdapter();
            $query="INSERT INTO `user` ( `id` , `uname` , `password` )
            VALUES ('', '{$uname}', '{$pass}');";
            $db->query($query);         

        }}

i want to implement db-table with the above code .i am new to zend frame work i spend some time on reading programmers guide but in vain .It is very hard to get things from manual.So please some one explain step by step procedure for implementing the same .Thanks in advanced.

2
  • Did you see the Zend Framework Quick Start yet? That shows how to use the table data gateway pattern to create models and mappers for your data. It's where I learned ZF for the most part. Once you get through that, the reference guide is a lot easier to understand. Commented Jul 18, 2012 at 1:59
  • I reckon it is little too late to start learning ZF1, it's been 5 years since first version was released, and will take couple years more to get deep understanding of ZF. Commented Jul 18, 2012 at 2:27

1 Answer 1

1

Ok here is a quick example:

Create DbTable Model (Basically an adapter for each table in your database):

at the command line: zf create db-table User user add -m moduleName if you want this in a module. This command will create a file name User.php at /application/models/DbTable that looks like:

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract {

    protected $_name = 'user'; //this is the database tablename (Optional, automatically created by Zend_Tool)
    protected $_primary = 'id'; //this is the primary key of the table (Optional, but a good idea)
}

now to create a method in your DbTable model that executes the query from your controller, add a method similar to:

public function insertUser(array $data) {
    $Idata  = array(
        'name' => $data['name'] ,
        'password' => $data['passowrd']
    );
    $this->insert($Idata); //returns the primary key of the new row.
}

To use in your controller:

public function adduserAction(){
        $form = new Application_Form_Auth(); //prepare form          
try{
        if($this->getRequest()->isPost()){//if is post
            if ($form->isValid($this->getRequest()_>getPost()){ //if form is valid
                $data = $form->getValues(); //get filtered and validated form values
                $db= new Application_Model_DbTable_User(); //instantiate model
                $db->insertUser($data); //save data, fix the data in the model not the controller
                //do more stuff if required
     }     
   }
     $this->view->form = $form; //if not post view form
     $this->view->assign('a','Entere new user:');
  } catch(Zend_Exception $e) { //catach and handle exception
        $this->_helper->flashMessenger->addMessage($e->getMessage());//send exception to flash messenger
        $this->_redirect($this->getRequest()->getRequestUri()); //redirect to same page
  }
}

This is about as simple as it gets. I have barely scratched the surface of what can be accomplished in simple applications with Zend_Db and a DbTable model. However you will likely find as I have, that at some point you will need more. That would be the time to explore the domain model and mappers.

For a really good basic tutrial that includes all of this and more please check out Rob Allen's ZF 1.x tutorial

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.