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 ???
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.
$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