-1

I am new to ZF. I have no idea about the flow of ZF MUCH. What I do is that I am coding my models class work too in my controllers action. That works pretty well, but I don't like this. I want ZF style. Below is my code I mean my one action on my controller. Could you please retouch it by making a model class and cut paste the code there. And then how can I invoke that class? How can I pass variables there? And how can I retrieve result from there and assign that result to my view?

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 );
       }
    }

please edit my code .Thanking you in aniticipation

Edited:: i have add model which is like this

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();
    }
   }
 ?>

than change my Action to this

  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 );
      }

but this is giving thsi error

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

Any idea sir ???

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53
  • 1
    The query to the database should be in a model. Anyway I suggest you to study better what is MVC. – Aurelio De Rosa Feb 03 '12 at 13:08
  • @AurelioDeRosa i will sir but now i need it so plz retouch my code plz – Fawad Ghafoor Feb 03 '12 at 13:12
  • @AurelioDeRosa and how can i pass $phone_service_id to that class coz query works on it – Fawad Ghafoor Feb 03 '12 at 13:13
  • Create a model where you will do the query. Then return the results in the form that better fits your needs. Then, I usually assign var to the view in this way: $this->view->Url = $request->getBaseURL() . Then, in the view, you can use it in this way $this->Url – Aurelio De Rosa Feb 03 '12 at 13:23
  • Contributing to @AurelioDeRosa, look here: http://www.phpactiverecord.org/projects/main/wiki/Quick_Start. You should have the query in a seperate model – Abe Petrillo Feb 03 '12 at 13:24
  • where is this: Application_Model_Services, located and what is the file name? It should be located at /application/models/Services.php for the autoloader to find it. However using the ZF conventions this file should be at /application/models/dbtable/Services.php and be called Application_Model_DbTable_Services – RockyFord Feb 04 '12 at 06:43

1 Answers1

2

Can't retouch your code. You have to much going on here. If you want to address one issue at a time, fine.
I will give you a few tips that helped me when I was newer to ZF.

  • RTFM - Do the quickstart in the docs and pay attention. i don't mean read it. I mean type it out and make it work.
  • Do Rob Allen's tutorial at ZF 1.11 tutorial, same way type it out and make it work.
  • Put everything you can into the application.ini. (DB settings, library namespaces...)
  • When you are doing the tutorials pay close attention to how to use the Zend_Tool command line interface. This will make things very easy and at least Netbeans and Zend Studio have an interface for it.
  • If you are not planning on using a third party ORM learn about the Application_Model_DbTable_ modles and how they work and what extending Zend_Db_Table_Abstract get you.
  • One last thing. To asign data to a view the common syntax is $this->view->data = $data; to display that data in the view script - <?php echo $this->data ?>

Other free resources:
ZF Manual USE IT!!!
Survive the Deepend, A free online book
Models, Tables and Relationships in ZF

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • @FawadGhafoor you seem like a genuine guy who is trying to learn how to fit into the SO community. However, using the phrase 'help me' so often makes you come across as a [help vampire](http://slash7.com/2006/12/22/vampires/), which may put people off helping you. People will help, that's why they come here, just be patient :) – vascowhite Feb 03 '12 at 16:16