When creating database models using zend_db_table you need to extend to
abstract classes: zend_db_table_abstract and zend_db_row_abstract.

Take the following simple scenario if you have just a user and a user can
post many mesages.

so we could have:

user_id {primary key}
username
fname
lname

and 

message_id {primary key}
user_id {foreign key}
message
date_time

something like that....

Anyways what you would need then is 4 models like this

class Models_DbTable_Users extends Zend_Db_Table_Abstract
{
           // this is the table model
}

class Models_DbTable_User extends Zend_Db_Row_Abstract
{
        // This is the row model - ie for an individual user or row
}

class Models_DbTable_Messages extends Zend_db_Table_Abstract
{
      // messages model
}

class Models_DbTable_Message extends Zend_Db_Row_Abstract
{
      // individual message
}

You then need to do some basic configuration so that Zend_Db_table knows how
these classes should interact. So we would need to:

class Models_DbTable_Users extends Zend_Db_Table_Abstract
{
        protected $_name = 'users';   // table name
        protected $_primary = 'user_id';     // primary key field
        protected $_rowClass = 'Models_DbTable_User';  // class name of the row
class

        // Any tables that use the user_id as a foreign key are said to be
dependent tables
        protected $_dependentTables = array('Models_DbTable_Messages');
}

We also need to setup the messages model like so:

class Models_DbTable_Messages extends Zend_Db_Table_Abstract
{
        protected $_name = 'messages';   // table name
        protected $_primary = 'message_id';     // primary key field
        protected $_rowClass = 'Models_DbTable_Message';  // class name of the 
row
class

        // We use a reference map for foreign keys
        protected $_referenceMap = array(
                'MessagesByUser' => array(                // relationship name 
- can be
anything you like
                        'columns' => array('user_id'),      // column in this 
table that holds
foreign key
                        'refTableClass' => 'Models_DbTable_Users',  // 
Reference Class Model
                        'refColumns' => array('user_id')  // reference column - 
primary key in
the user table
                )
        );
}

Now we have our basic config out of the way we can implement our own methods
in the model to keep our controllers as lightweight as possible (fat model,
thin controller).

If we want to get all messages by a specific user we could do this

class Models_DbTable_User extends Zend_Db_Row_Abstract
{
        public function getAllMessages()
        {
              return $this->findDependentRowSet('Models_DbTable_Messages');
        }
}

Now, in your controller if you wanted to get all messages by a user you
would only need to do this:

$users = new Models_DbTable_Users();
$user = $users->find(10)->current(); // find user with id 10
$messages = $user->getAllMessages(); // get all messages by user
$this->view->messages = $messages; // assign to view

Hope this helps a little bit.

Matt

}


remataklan wrote:
> 
> I was trying to understand the quickstart guide on the ZF site. 
> 
> My question is about the model part.
> First of all a little check to see if I understand the stuff. As I
> understand Zend_Db_Table handles the stuff with database. The mapper maps
> this to the model and the model handles everything about getting and
> setting data. Is this correct?
> 
> Now the question, is it me or in the end the data we passed to the view is
> a little bit large. I mean the whole model object is passed to the final
> view. Is this the correct way to do it?
> 
> And finally if I wanted to find to all posts from a certain user do I need
> a new function in the mapper? Something like 
>     
> public function postsByUser($userId)
>     {
>         $resultSet =
> $this->getDbTable()->fetchAll($this->getDbTable()->select()->where('user_id
> = ?', $userId));
> 
>         $entries   = array();
>         foreach ($resultSet as $row) {
>             $entry = new Model_List();
>             $entry->setId($row->id)
>                   ->setEmail($row->email)
>                   ->setComment($row->comment)
>                   ->setCreated($row->created)
>                   ->setMapper($this);
> 
>             $entries[] = $entry;
>         }
>         return $entries;
>     }
> 
> or is there better way.
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Zend-Model-and-quickstart-tp23445168p23445836.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to