【问题标题】:How to implement data model with this code如何使用此代码实现数据模型
【发布时间】:2012-07-18 01:49:52
【问题描述】:

我是 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 框架的新手,我花了一些时间阅读程序员指南,但徒劳无功。从手册中获取内容非常困难。所以请一些解释步骤执行相同的分步程序。在此先感谢。

【问题讨论】:

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

标签: zend-framework


【解决方案1】:

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

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

在命令行:zf create db-table User user 添加-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.
}

在您的控制器中使用:

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

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 2021-06-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多