SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2004
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zend Framework and Model

    Messing around with the Zend Framework and just wondering how everyone is handleing the model when it comes to the mvc part of the framework. This is what i have so far. Not new to OOP but still have some way to go. Any insight would be greatly appreciated.

    I removed the view (html) from this as well as the comments to save space since its pretty straight forward at this stage and the main goal i hope to accomplish is get input on how people are passing the model into the controller.


    index.php (Index)
    PHP Code:
    <?php
    require_once 'Zend.php';
     
    function 
    __autoload($class)
    {
        
    Zend::loadClass($class);
    }

    $config = new Zend_Config(Zend_Config_Xml::load('../app/config/config.xml''production'));

    $params = array('host'     => $config->database->host,
                    
    'username' => $config->database->username,
                    
    'password' => $config->database->password,
                    
    'dbname'   => $config->database->name);
    $db Zend_Db::factory('PDO_MYSQL'$params);
    Zend::register('db'$db);

    $router = new Zend_Controller_RewriteRouter();
    $router->addRoute('user''users/:username', array('controller' => 'users''action' => 'index'));

    $ctrl Zend_Controller_Front::getInstance();
    $ctrl->setControllerDirectory('../app/controllers/');
    $ctrl->setRouter($router);
    $ctrl->dispatch();

    UsersController.php (Controller)
    PHP Code:
    <?php
    require_once '../app/models/Users.php';

    class 
    UsersController extends Zend_Controller_Action 
    {
        private 
    $_users;
        private 
    $_view;
        
        public function 
    __construct()
        {
            
    $this->_users = new Users();
            
    $this->_view = new Zend_View();
            
    $this->_view->setScriptPath('../app/views/');
        }
        
        public function 
    indexAction()
        {
            
    $params $this->_getAllParams();
            if (
    $params['username']) {
                
    $this->infoAction();
            } else {
                
    $this->_view->users $this->_users->getUsers();
                echo(
    $this->_view->render('users.php'));
            }
        }
        
        private function 
    infoAction()
        {
            
    $this->_view->userInfo $this->_users->getInfo();
            echo(
    $this->_view->render('userInfo.php'));
        }
    }

    Users.php (Model)
    PHP Code:
    <?php
    class Users
    {
        private 
    $_db;
        
        public function 
    __construct()
        {
            
    $this->_db Zend::registry('db');
        }
        
        public function 
    getUsers()
        {
            
    $sql "SELECT user
                    FROM users"
    ;
            if (
    $result $this->_db->query($sql)) {
                return 
    $result->fetchAll();
            }
        }
        
        public function 
    getInfo()
        {
            
    $sql "SELECT first_name, last_name
                    FROM users"
    ;
            if (
    $result $this->_db->query($sql)) {
                return 
    $result->fetchAll();
            }
        }
    }

  2. #2
    SitePoint Member
    Join Date
    Oct 2004
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Oct 2004
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nothing?

    any refrences would be appreciated.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Two points
    1. Consider passing $db into the constructor. This removes the dependency on Zend::registry and allows for using multiple databases should the need arise. Not as big of a hassle as you might think.

    2. Unless you have a bunch more functionality in your Users class then just extend Zend_Db_Table and go on from there.

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Worcester
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I extend Zend_Db_Table for the "fetching" side. The trouble is that Zend_Db_Table returns its results as Zend_Db_Rowsets that you could really do with overriding for the rest of the specific stuff you need.

  6. #6
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by akrabat
    I extend Zend_Db_Table for the "fetching" side. The trouble is that Zend_Db_Table returns its results as Zend_Db_Rowsets that you could really do with overriding for the rest of the specific stuff you need.
    Allowing one to specify the class for return rows and row sets has been asked for a long time ago. It's easy to extend Zend_Db_Table to allow:
    PHP Code:
    class AccountRow extends Mine_Db_Table_Row
    {    
    }
    class 
    AccountRows extends Mine_Db_Table_Rows
    {
        protected 
    $_rowClassName 'AccountRow';
    }
    class 
    AccountTable extends Mine_Db_Table 
    {
        protected 
    $_name    'account';
        protected 
    $_primary 'account_id';
        
        protected 
    $_rowClassName    'AccountRow';
        protected 
    $_rowsetClassName 'AccountRows'
    However, in practice I find myself mainly using Db_Table with the default row type for simple CUD operations and using custom queries for most of the retrievals.

  7. #7
    SitePoint Member
    Join Date
    Oct 2004
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your responses.

    As for this example this is just an example app to see how zf works. Thank you for pointing out Zend_Db_Table as i have yet to take a real in depth look at this class.

    Right now the main goal is to get the basic outlining architecture of the framework. This is where the question of how to pass the model to the controller comes from. I have seen a lot of "tutorials" do everything inside the controller methods including the IBM/Zend tutorial which I find odd they would place that much code in one class.

    Is an include and an instance of the model (passing in $db to constructor) the best way to get to the model from inside the controller?

  8. #8
    SitePoint Member
    Join Date
    Jun 2006
    Location
    Zagreb, Croatia
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a little bit offtopic. But I think you would be better if you would register a view in Index.php (Index), something like:

    PHP Code:
    $view = new Zend_View;
    $view->setScriptPath('../app/views');
    Zend::register('view'$view); 
    And than just use
    PHP Code:
    $view Zend::registry('view'); 
    in your __construct() method of the controller.

    If your controller needs a view from another directory (not the one specified in your FrontController) you can add it in the __contruct() method on per-controller basis. ZF is smart enough to first look in the last added directory, and if it doesnt find the view file there it keeps looking at other directorys you might have added during the script execution.

  9. #9
    SitePoint Member
    Join Date
    Oct 2004
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Initially I was registering the view but since my views directory has multple sub directories i dropped that style due to the following:

    PHP Code:
    $view = new Zend_View;
    $view->setScriptPath('../app/views');
    Zend::register('view'$view); 

    ...

    $this->_view =  $view Zend::registry('view'); 
    $this->_view->setScriptPath('../app/views/users/'); 
    vs.

    PHP Code:
    $this->_view = new Zend_View();
    $this->_view->setScriptPath('../app/views/'); 
    It removes a dependancy from the view class but creates one to the registry class.
    It takes less lines of code to create an instance and set path inside the 2nd cotroller.

    But of course maybe im missing something so please fill me in if i am.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •