2012-07-18 42 views
0

我是新來的zend框架工作。我試圖學習zf創建db-table tblename,zf創建模型tblname,zf創建模型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);   

     }} 

我想要實現與上面的代碼分貝表。我是zend框架新手,我花了一些時間閱讀程序員指南,但徒勞無功。很難從手動獲取東西所以請一些人解釋一步步執行相同的步驟。感謝高級。

+0

您有沒有看到[Zend Framework Quick Start](http://framework.zend.com/manual/en/learning.quickstart.intro.html)?這表明如何使用表格數據網關模式爲您的數據創建模型和映射器。這是我大部分學習ZF的地方。一旦你瞭解了這些,參考指南很容易理解。 – drew010 2012-07-18 01:59:29

+0

我認爲現在開始學習ZF1已經太遲了,自從第一版發佈以來已經有5年了,爲了深入理解ZF,還需要幾年的時間。 – 2012-07-18 02:27:06

回答

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

我希望這可以幫助一個非常良好的基礎tutrial。