Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-19-2008, 02:50 PM
Member
 
Join Date: Feb 2008
Posts: 48
Question Zend_Model is not there.?

Hai all,

I understood the Controller and the View Creation. But i could not identify the Model files in this ZFwork. can any one tell me how we can add this Model? and how we can call the function from there?

Thanks
Mugesh.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-25-2008, 02:18 PM
Junior Member
 
Join Date: Feb 2008
Posts: 4
Default

hi
you can create a model link following then save in path of your project/application/models :
PHP Code:
<?php
class Blog 
{
    private 
$db;
    
    public function 
__construct()
    {
        
$this->db Zend_Registry::get('db');
    }
    
    
/**
     * Enter description here...
     *
     * @return object
     */
    
public function fetchAllPosts()
    {
        
$sql 'SELECT 
                         p.post_id,
                         t.name, 
                         p.title,
                         p.body,
                         p.date,
                         p.date_modify,
                         p.hits,
                         p.allow_comment
                FROM post AS p,
                     tag AS t
                WHERE
                      p.tag_id=t.tag_id 
                ORDER BY post_id DESC'
;
        
        
$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
        
$result $this->db->fetchAll($sql);
        return 
$result;
    }

    
//others methods
}
?>
now you can use this model vai following :
PHP Code:
<?php
class IndexController extends Zend_Controller_Action 
{
    public function 
init()
    {
        
Zend_Loader::loadClass('Blog');
        
$this->view->baseUrl $this->_request->getBaseUrl();
    }
    
    public function 
indexAction()
    {
        
$blog = new Blog();
        
$this->view->title "My Album";
        
$this->view->posts $blog->fetchAllPosts();
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-25-2008, 04:00 PM
Member
 
Join Date: Jun 2007
Posts: 33
Default

You can just extend the db_table_abstract, but I find that zamanphp's method is much better. The one change I would make is that I create the db class from the registery at the top of my controller, then pass it as a reference to all my models. I would change the controller to this:

PHP Code:
<?php
class IndexController extends Zend_Controller_Action 
{
    public function 
init()
    {
        
Zend_Loader::loadClass('Blog');
        
$this->db Zend_Registry::get('db'); 
        
$this->view->baseUrl $this->_request->getBaseUrl();
    }
    
    public function 
indexAction()
    {
        
$blog = new Blog(&$this->db);
        
$this->view->title "My Album";
        
$this->view->posts $blog->fetchAllPosts();
    }
}
Which would change the model to this:
PHP Code:
<?php
class Blog 
{
    private 
$db;
    
    public function 
__construct($db)
    {
        
$this->db $db;
    }

    
//Other methods
}

Last edited by Jhorra : 02-25-2008 at 04:04 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-13-2008, 10:19 PM
Junior Member
 
Join Date: Feb 2008
Posts: 17
Default

This is very helpful, thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:56 AM.