动态获取数据功能

时间:2013-08-08 10:40:11

标签: mysql zend-framework

我已经在zend framework中创建了简单的数据库应用程序。我已经创建了用户类并为数据库交互定义了不同的函数。这个函数是从控制器类调用的。但是我必须声明每个类对象用于调用方法每个action.h中的用户类。我可以一次声明用户类对象并在控制器的所有操作方法中使用。     这是我的控制器页面     class UserController扩展了Zend_Controller_Action     {

    public function init()
    {
         /* Initialize action controller here */
        Zend_Session::start();

    }

     public function indexAction()
    {

        $title = Zend_Registry::get('title');
        $this->view->assign('name', 'Wiwit');
        $this->view->assign('title', $title);
    }


    public function logoutAction()
    {

        Zend_Session::destroy();
        $this->redirect("/user/login");

    }

     public function registerAction()
    {

        $this->view->assign('title','Register');
        $request = $this->getRequest();
        $this->view->assign('action', $request->getBaseURL()."/user/register");
        if($request->isPost()) 
        {
        $data = array('first_name' => $request->getParam('first_name'),
                      'last_name' => $request->getParam('last_name'),
                      'user_name' => $request->getParam('user_name'),
                      'password' => $request->getParam('password')
                      );

        $user = new Application_Model_User();
        $rows_affected=$user->user_insert($data);
        $this->redirect("/user/login");
        }

    }
    public function loginAction()
        {
            if(isset($_SESSION['username']))
            {
                $this->redirect("/user/home");
            }
            $this->view->assign('title','Register');
            $request = $this->getRequest();
            $this->view->assign('action', $request->getBaseURL()."/user/login");
            if($request->getParam('msg'))
            {

                $this->view->msg='username or password is invalid';
            }
            if($request->isPost()) 
            {
                $data = array(
                              'user_name' => $request->getParam('user_name'),
                              'password' => $request->getParam('password')
                              );

                $user = new Application_Model_User();
                $result=$user->user_login($data);
                if(empty($result)){

                $this->redirect("/user/login/msg/login failed");

            }
            else{
                $_SESSION['username']=$result->user_name;

                $this->redirect("/user/home");

            } 

            }
        }

        public function homeAction()
        {
            if(isset($_SESSION['username']))
            {
                $this->view->title='home';
                $request = $this->getRequest();
                $user = new Application_Model_User();
                $result=$user->user_grid();
                $this->view->rows=$result;
            }
            else
            {
                $this->redirect("/user/login");
            }
        }

         public function editAction()
        {

         $request = $this->getRequest();
         $id= $request->getParam("id");

        $user = new Application_Model_User();
        $result=$user->user_edit($id);

        $this->view->assign('data',$result);
        $this->view->action= $request->getBaseURL()."/user/update";

    }


    }


and here is my user class

<?php

class Application_Model_User extends Zend_Db_Table
{

      public function user_insert($data)    
      {   

            global $DB;  
         $rows_affected = $DB->insert('user', $data);
        return  $rows_affected;
      }

      public function user_login($data)    
      {       

        $u_name=array_shift($data);
        $pass=array_shift($data);
        $select = $this->_db->select()
                        ->from('user')
                        ->where("user_name = ?",$u_name)
                        ->where("password = ?",$pass);
                        $result = $this->getAdapter()->fetchRow($select);
                        return $result;


      }

       public function user_grid()    
      {  
         global $DB;    
        $sql = 'SELECT * FROM user';
        $stmt = $DB->query($sql);
        $result = $stmt->fetchall();
        return $result;
      }

      public function user_edit($data)    
      {       

        $select = $this->_db->select()
                        ->from('user')
                        ->where("id = ?",$data);
                        $result = $this->getAdapter()->fetchRow($select);
                        $data = (array) $result;
                        return $data;


      }

      public function userupdate($data,$id)    
      {   

        global $DB;  
        $rows_affected =$DB->update('user', $data,'id = '.$id);
        return  $rows_affected;
      }

       public function userdelete($id)    
      {   

        global $DB;  
        $rows_affected =$DB->delete('user','id = '.$id);
        return  $rows_affected;
      }
}
?>

1 个答案:

答案 0 :(得分:1)

您可以在init()方法中创建用户对象,并将其存储为类变量。

public function init()
{
     /* Initialize action controller here */
    Zend_Session::start();
    $this->user = new Application_Model_User();
}

然后它可用于任何动作方法。

public function homeAction()
{
    if(isset($_SESSION['username']))
    {
        $this->view->title='home';
        $request = $this->getRequest();
        $result = $this->user->user_grid(); // changed
        $this->view->rows=$result;
    }
    else
    {
        $this->redirect("/user/login");
    }
}