如何使用此代码实现数据模型

时间:2012-07-18 01:49:52

标签: zend-framework

我不熟悉框架工作。我正在尝试学习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框架工作的新手我花了一些时间阅读程序员指导但是徒劳无功。从手册中获取东西很难。所以请一些解释一步按步骤执行相同的程序。谢谢先进。

1 个答案:

答案 0 :(得分: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's ZF 1.x tutorial

我希望这会有所帮助。