Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:14:28+00:00 2026-05-31T17:14:28+00:00

I’ve been using ZF for few months and I’m really happy with it however

  • 0

I’ve been using ZF for few months and I’m really happy with it however I’m not completely sure about how to work with models relationships and at the same time avoid multiple queries to the db. Many people has this problem and no one seems to find a good solution for it. (and avoiding using a third party ORM) For example I have a list of users, and each user belongs to a group. I want a list of users displaying user info and group name (to tables: users, and groups. Users has a foreign key to the table groups).
I have:
2 mapper classes to handle those tables, UserMapper and GroupMapper.
2 Model Classes User and Group
2 Data Source classes that extends Zend_DB_Table_Abstract

in user mapper I can do findParentRow in order to get the group info of each user, but the problem is i have an extra query for each row, this is not good I think when with a join I can do it in only one. Of course now we have to map that result to an object. so in my abstract Mapper class I attempt to eager load the joining tables for each parent row using column aliasing (similar as Yii does.. i think) so I get in one query a value object like this
//User model object

$userMapper= new UserMapper();
$users= $userMapper->fetchAll(); //Array of user objects
echo $user->id;
echo $user->getGroup()->name // $user->getParentModel('group')->name // this info is already in the object so no extra query is required.

I think you get my point… Is there a native solution, perhaps more academic than mine, in order to do this without avoiding multiple queries? // Zend db table performs extra queries to get the metadata thats ok and can be cached. My problem is in order to get the parent row info… like in yii…. something like that $userModel->with(‘group’)->fetchAll();
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:14:29+00:00Added an answer on May 31, 2026 at 5:14 pm

    Develop your mapper to work with Zend_Db_Select. That should allow for flexibility you need. Whether group table is joined depends on the parameter provided to mapper methods, in this example group object is the critical parameter.

    class Model_User {
        //other fields id, username etc.
        //...
    
        /**
        * @var Model_Group
        */
        protected $_group;
    
        public function getGroup() {
            return $this->_group;
        }
    
        public function setGroup(Model_Group $group) {
            $this->_group = $group;
        }
    
    }
    
    class Model_Mapper_User {
    
        /**
        * User db select object, joins with group table if group model provided
        * @param Model_Group $group
        * @return Zend_Db_Select
        */
        public function getQuery(Model_Group $group = NULL) {
            $userTable = $this->getDbTable('user'); //mapper is provided with the user table
            $userTableName = $userTable->info(Zend_Db_Table::NAME); //needed for aliasing
            $adapter = $userTable->getAdapter();
    
            $select = $adapter->select()->from(array('u' => $userTableName));
    
            if (NULL !== $group) {
                //group model provided, include group in query
                $groupTable = $this->getDbTable('group');
                $groupTableName = $groupTable->info(Zend_Db_Table::NAME);
                $select->joinLeft(array('g' => $groupTableName), 
                                    'g.group_id = u.user_group_id');
            }
    
            return $select;
        }
    
        /**
        * Returns an array of users (user group optional)
        * @param Model_User $user
        * @param Model_Group $group
        * @return array
        */
        public function fetchAll(Model_User $user, Model_Group $group = NULL) {
            $select = $this->getQuery();
            $adapter = $select->getAdapter();
            $rows = $adapter->fetchAll($select);
    
            $users = array();
    
            if (NULL === $group) {
                foreach ($rows as $row) {
                    $users[] = $this->_populateUser($row, clone $user);
                }
            } else {
                foreach ($rows as $row) {
                    $newUser = $this->_populateUser($row, clone $user);
                    $newGroup = $this->_populateGroup($row, clone $group);
    
                    //marrying user and group
                    $newUser->setGroup($newGroup);
    
                    $users[] = $newUser;
                }
            }
    
            return $users;
        }
    
        /**
        * Populating user object with data
        */
        protected function _populateUser($row, Model_User $user) {
            //setting fields like id, username etc
            $user->setId($row['user_id']);
            return $user;
        }
    
        /**
        * Populating group object with data
        */
        protected function _populateGroup($row, Model_Group $group) {
            //setting fields like id, name etc
            $group->setId($row['group_id']);
            $group->setName($row['group_name']);
            return $group;
        }
    
        /**
        * This method also fits nicely
        * @param int $id
        * @param Model_User $user
        * @param Model_Group $group 
        */
        public function fetchById($id, Model_User $user, Model_Group $group = NULL) {
            $select = $this->getQuery($group)->where('user_id = ?', $id);
            $adapter = $select->getAdapter();
            $row = $adapter->fetchRow($select);
    
            $this->_populateUser($row, $user);
            if (NULL !== $group) {
                $this->_populateGroup($row, $group);
                $user->setGroup($group);
            }
    
            return $user;
        }
    
    }
    

    use scenarios

    /**
     * This method needs users with their group names 
     */
    public function indexAction() {
        $userFactory = new Model_Factory_User();
        $groupFactory = new Model_Factory_Group();
        $userMapper = $userFactory->createMapper();
        $users = $userMapper->fetchAll($userFactory->createUser(), 
                                            $groupFactory->createGroup());
    }
    
    /**
     * This method needs no user group
     */
    public function otherAction() {
        $userFactory = new Model_Factory_User();
        $userMapper = $userFactory->createMapper();
        $users = $userMapper->fetchAll($userFactory->createUser());
    }
    

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.