0

For example:

I need to retrieve a set of male User's IDs, First Names, and Last Names, but nothing else.

So I have a function in UserMapper called fetchAllMaleUsers() that returns a set of User entities.

i.e:

public function fetchAllMaleUsers() {
        $select = $this->getDbTable()
                        ->select()
                        ->from($this->getDbTable(),
                                array('ID', 'FirstName', 'LastName'))
                        ->where('Gender = ?', 'M');

        $resultSet = $this->getDbTable()->fetchAll($select);

        $users = array();
        foreach ($resultSet as $row) {
            $user = new Application_Model_User();
            $user->setId($row->ID)
                 ->setFirstName($row->FirstName)
                 ->setLastName($row->LastName);
            $users[] = $user;
        }

        return $users;
    }
  1. Does this function belong in the mapper layer?
  2. Is it ok to only set the Id, Firstname, and LastName of each User entity?

2 Answers 2

1

1) Perfectly fine for the mapper/Gateway or however you call it.
2) You can do but i highly disencourage this. Why? Later on in your application you can't tell from where you did get the model. So you'd have to check if a value is set in your model each time you're not sure if it is set or you'd need some autoloading stuff for missing values (which is as worse as missing stuff). Another reason is that you can't reuse the function for other porposes where you might need other properties of the user model. Last reason afaik is, that a model represents the complete entity at any time, not parts of it. And there's no real reason why not to load all fields (beside references to other entities why should be autoloaded anyways).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was just hesitant because querying for every column seems like an unnecessary calculation for the db. But maybe that's not a problem. So whenever I try to get a property of a User entity I should map every column to a User entity?
@Craig: Yes. Interesstingly people tend optimize at the wrong places at first. Fetching additional columns from the same db-table is almost no overhead (of course if you store binary data there...). If you want to optimize your application the zend framework and your php code is a good start (and any more complex db-queries or db-layout in general). A good start to get the idea behing the mode/mapper stuff are (and for your other comment) are survivethedeepend.com/zendframeworkbook/en/1.0 and weierophinney.net/matthew/archives/…
0

Along with Fge's reply I believe that $resultSet should already be returning values of type Application_Model_User. If not, you may need to set $_rowClass in Application_Model_DbTable_User.

3 Comments

Hmm, I'm basing this off Zend Framework's quick-start which under fetchAll() in Application_Model_GuestbookMapper also iterates through $resultSet, mapping the values to a new Application_Model_Guestbook. Is this not best practices, and is there a more in depth resource on mapper and domain layers in ZF that you'd recommend?
The links that Fge posted should cover the mapper part. All I'm saying is that the model, depending on the fetch mechanism used, will auto-create a populated model object.
I'm not 100% sure, but I believe you must extend Zend_Db_Table_Row_Abstract in order to use a class as $_rowClass? This would mean that the model gets knowledge of the underlying DB schema, which is what the Data Mapper pattern tries to avoid. But of course you have to weigh that against iterating over the result set just to create the model objects...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.