Thanks for the replies.
I was trying to something very close to your example. However I ran into
problem.
I am using the autoloader suggested in the quickstart, something like this
protected function _initAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
return $moduleLoader;
}
My class name is class Model_DbTable_Users extends Zend_Db_Table_Abstract
and there is no problem with loading the class.
However when I set
protected $_rowClass = 'Model_DbTable_User';
It doesnt load instead throws and exception
Message: File "Model\DbTable\User.php" does not exist or class
"Model_DbTable_User" was not found in the file
Stack trace:
#0 C:\Apache2\zf\gifted\library\Zend\Db\Table\Rowset\Abstract.php(119):
Zend_Loader::loadClass('Model_DbTable_U...')
#1 C:\Apache2\zf\gifted\library\Zend\Db\Table\Abstract.php(1223):
Zend_Db_Table_Rowset_Abstract->__construct(Array)
#2 C:\Apache2\zf\gifted\library\Zend\Db\Table\Abstract.php(1177):
Zend_Db_Table_Abstract->fetchAll('((`user`.`id` =...')
#3 C:\Apache2\zf\gifted\application\controllers\ListController.php(23):
Zend_Db_Table_Abstract->find(1)
#4 C:\Apache2\zf\gifted\library\Zend\Controller\Action.php(512):
ListController->userAction()
#5
C:\Apache2\zf\gifted\library\Zend\Controller\Dispatcher\Standard.php(288):
Zend_Controller_Action->dispatch('userAction')
#6 C:\Apache2\zf\gifted\library\Zend\Controller\Front.php(936):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http),
Object(Zend_Controller_Response_Http))
#7
C:\Apache2\zf\gifted\library\Zend\Application\Bootstrap\Bootstrap.php(77):
Zend_Controller_Front->dispatch()
#8 C:\Apache2\zf\gifted\library\Zend\Application.php(303):
Zend_Application_Bootstrap_Bootstrap->run()
#9 C:\Apache2\htdocs\gifted\index.php(20): Zend_Application->run()
#10 {main}
I dont know if its some kind of bug or something I failed to see. Any help?
:)
yidarmy2009 wrote:
>
> 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-tp23445168p23482117.html
Sent from the Zend Framework mailing list archive at Nabble.com.