如何在Zend Framework中使我的代码更像MVC

时间:2012-02-03 13:05:08

标签: performance zend-framework optimization zend-db

我是ZF的新手。我不知道ZF MUCH的流量。我所做的就是我在控制器动作中编写了我的模型类工作。这很好用,但我不喜欢这个。我想要ZF风格。下面是我的代码我的意思是我的控制器上的一个动作。你可以通过制作一个模型类来修饰它,并在那里剪切粘贴代码。然后我该如何调用该类?我怎样才能在那里传递变量?如何从那里检索结果并将结果分配给我的视图?

public function calllogsAction(){
         if(!Zend_Auth::getInstance()->hasIdentity()){
               $this->_redirect('login/login');
      }
      else{

       $request = $this->getRequest();
       $phone_service_id  =  $request->getParam("id");
       $registry = Zend_Registry::getInstance();  
       $DB = $registry['DB'];

       $select = $DB->select()
         ->from('CALL_LOG', array('caller_name','call_number','call_start_time','call_duration','call_direction'))
         ->where('phone_service_id = ?', $phone_service_id)
         ->order('date_created DESC')
         ->limit(0,9);

       $adapter = new Zend_Paginator_Adapter_DbSelect($select);
       $paginator = new Zend_Paginator($adapter);
       $page=$this->_getParam('page',1);
       $paginator->setItemCountPerPage(10);
       $paginator->setCurrentPageNumber($page);
       $this->view->paginator=$paginator;
       $page = $paginator->getCurrentPageNumber();
       $perPage = $paginator->getItemCountPerPage();
       $total = $paginator->getTotalItemCount();
       $A = ($page - 1) * $perPage + 1;
       $B = min($A + $perPage - 1, $total);
       $C = $total;
       $this->view->assign('url', $request->getBaseURL());
       $this->view->assign('total',$total );
       $this->view->assign('page',$page );
       $this->view->assign('phone_service_id',$phone_service_id );
       $this->view->assign('A',$A );
       $this->view->assign('B',$B );
       $this->view->assign('C',$C );
       }
    }

请编辑我的代码。感谢您参与

编辑:: 我添加了这样的模型

class Application_Model_Services extends Zend_Db_Table_Abstract{

protected $_name = 'albums';

    public function get_services($user_id,$DB){
    $user_id = (int)$user_id;

     $select = $DB->select()
     ->from(array('p' => 'phone_service'))
     ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
     ->where('u.user_preferences_name = ?', 'is_user_package_active')
     ->where('p.user_id = ?', $user_id);

     $adapter   = new Zend_Paginator_Adapter_DbSelect($select);
     $paginator = new Zend_Paginator($adapter);

    if (!$paginator) {
        throw new Exception("Could not find row ");
    }
    return $paginator->toArray();
    }
   }
 ?>

将我的行动改为此

  public function controlpannelAction(){
      if(!Zend_Auth::getInstance()->hasIdentity()){
          $this->_redirect('login/login');
      }
      else{
      $data = Zend_Auth::getInstance()->getStorage()->read();  
      $user_id = $data->user_id;
      $registry = Zend_Registry::getInstance();  
      $DB = $registry['DB'];

       $services = new Application_Model_Services();
       $paginator = $services->get_services($user_id,$DB);


     $page=$this->_getParam('page',1);
     $paginator->setItemCountPerPage(10);
     $paginator->setCurrentPageNumber($page);
     $this->view->paginator=$paginator;
     $request = $this->getRequest();
     $this->view->assign('url', $request->getBaseURL());
     $page = $paginator->getCurrentPageNumber();
     $perPage = $paginator->getItemCountPerPage();
     $total = $paginator->getTotalItemCount();
     $A = ($page - 1) * $perPage + 1;
     $B = min($A + $perPage - 1, $total);
     $C = $total;
     $this->view->assign('A',$A );
     $this->view->assign('B',$B );
     $this->view->assign('C',$C );
      }

但是这给出了错误

Warning: Zend_Loader::include_once(Application\Model\Services.php) [function.Zend-Loader-include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\xyz\library\Zend\Loader.php on line 146

Warning: Zend_Loader::include_once() [function.include]: Failed opening 'Application\Model\Services.php' for inclusion (include_path='C:\xampp\htdocs\phoggi/library;.;C:\xampp\php\pear\') in C:\xampp\htdocs\xyz\library\Zend\Loader.php on line 146

Fatal error: Class 'Application_Model_Services' not found in C:\xampp\htdocs\xyz\application\controllers\LoginController.php on line 61

任何想法先生???

1 个答案:

答案 0 :(得分:2)

无法修饰您的代码。你必须在这里继续努力。如果你想一次解决一个问题,那很好  当我刚接触ZF时,我会给你一些帮助我的提示。

  • RTFM - 在文档中快速入门并注意。我不是说读它。我的意思是输入它并使其工作。
  • ZF 1.11 tutorial做Rob Allen的教程,同样的方式输入并使其有效。
  • 将您可以使用的所有内容放入application.ini中。 (数据库设置,库名称空间......)
  • 当您进行教程时,请密切关注如何使用Zend_Tool命令行界面。这将使事情变得非常简单,至少Netbeans和Zend Studio有一个接口。
  • 如果您不打算使用第三方ORM了解Application_Model_DbTable_模块及其工作方式以及Zend_Db_Table_Abstract扩展的内容。
  • 最后一件事。要将数据标注到视图,常见语法为$this->view->data = $data;,以便在视图脚本中显示该数据 - <?php echo $this->data ?>

其他免费资源:
ZF Manual使用它!!! Survive the Deepend, A free online book
Models, Tables and Relationships in ZF