Zend framework 如何用这段代码实现数据模型

Zend framework 如何用这段代码实现数据模型,zend-framework,Zend Framework,我是zend框架的新手。我试图学习zf create db table tblename、zf create model tblname、zf create model TBLNAMEMAPER 添加user.php(表单) 管理员控制器: <?php class AdminController extends Zend_Controller_Action { public function adduserAction(){ $form = new

我是zend框架的新手。我试图学习zf create db table tblename、zf create model tblname、zf create model TBLNAMEMAPER

添加user.php(表单)


管理员控制器:

<?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);         

        }}
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
  }
}

好这里是一个简单的例子:

创建DbTable模型(基本上是数据库中每个表的适配器):

在命令行:
zf create db table User User
add
-m moduleName
,如果您想在模块中使用它。此命令将在
/application/models/DbTable
处创建一个文件名
User.php
,如下所示:

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)
}
现在,要在DbTable模型中创建一个从控制器执行查询的方法,请添加类似以下内容的方法:

public function insertUser(array $data) {
    $Idata  = array(
        'name' => $data['name'] ,
        'password' => $data['passowrd']
    );
    $this->insert($Idata); //returns the primary key of the new row.
}
要在控制器中使用:

<?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);         

        }}
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
  }
}
这是最简单的。我几乎没有触及使用Zend_Db和DbTable模型在简单应用程序中可以实现的功能的表面。然而,你可能会像我一样发现,在某个时候,你需要更多。这将是探索域模型和映射器的时候了。

要获得包含所有这些以及更多内容的非常好的基础教程,请查看


我希望这能有所帮助。

您看到了吗?这显示了如何使用表数据网关模式为数据创建模型和映射器。这是我学习ZF的主要地方。一旦你了解了这一点,参考指南就更容易理解了。我认为现在开始学习ZF1有点太晚了,第一个版本发布已经5年了,要想深入理解ZF还需要几年的时间。