0

我是 zend 框架的新手。我正在尝试学习 zf create db-table tblename,zf create model tblname,zf create model tblnameMapper。

添加 user.php(表单)

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

添加用户视图:

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

?>

管理员控制器:

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

        }}

我想用上面的代码实现 db-table。我是 zend 框架的新手,我花了一些时间阅读程序员指南但徒劳无功。从手册中获取东西非常困难。所以请有人一步一步解释过程实施相同的。谢谢提前。

4

1 回答 1

1

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

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

在命令行:如果你想在模块中zf create db-table User user添加它。-m moduleName此命令将创建一个文件名User.php/application/models/DbTable如下所示:

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

要在您的控制器中使用:

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 模型的简单应用程序中可以完成的事情的皮毛。但是,您可能会像我一样发现,在某些时候您将需要更多。那将是探索领域模型和映射器的时候了。

如需包含所有这些以及更多内容的非常好的基本教程,请查看Rob Allen 的 ZF 1.x 教程

我希望这有帮助。

于 2012-07-18T03:42:05.670 回答