Well I am using 1.8 but I haven't done any database stuff with 1.8 yet. I was
just helping using what I remembered from what I did a couple of months ago.
So I guess you could be right about it being a bug or something because that
definitely should work as far as I can tell. Weird, maybe post an issue
about it?
remataklan wrote:
>
> Well the namespace/filesystem works for both classes when calling them
> directly. The addResourceType works as expected when I instatiate the item
> directly. However it seems that when there is another converter somewhere
> which converts the directory name as "Model" instead of "Models". But I
> cannot seem to find where it is.
>
> I am suspecting it is somewhere:
>
> ...\library\Zend\Db\Table\Rowset\Abstract.php(119):
> Zend_Loader::loadClass('Model_DbRow_Use...')
>
> I'm still thinking it may be a bug. Because I checked for typos multiple
> times. Which version are you using?
>
>
> yidarmy2009 wrote:
>>
>> Yes, you are correct you don't actually call the user class directly.
>> Zend_Db_table does that when you have a result object with 1 single row
>> (normally you have to call results->current() to ensure that you do have
>> a single row).
>>
>> Anyways, surely if your current namespace/file system works for the Users
>> class it should work for the User class. Just make sure whatever naming
>> convention you have used for the Users class you have used in the User
>> class.
>>
>> So if your using Model_DbTable_Users make sure you also use
>> Model_DbTable_User. It might even be better to have a folder
>> models/dbRow/User and use Model_DbRow_User. If you did that, however, i
>> guess you would need to create a custom resource loader in your bootstrap
>> like:
>>
>> protected function _initResources()
>> {
>> $resourceloader = new Zend_Loader_Autoloader_Resource(array(
>> 'namespace' => ''
>> 'basePath' => APPPLICATION_PATH
>> ));
>>
>> $resourceloader->addResourceType('model', 'models/', 'Model');
>> $resourceloader->addResourceType('dbTableModel', 'models/dbTable/',
>> 'Model_DbTable');
>> $resourceloader->addResourceType('dbTableRow', 'models/dbRow',
>> 'Model_DbRow');
>> }
>>
>> Other than that just ensure the class name you use for the $_rowClass is
>> correct... ie if you did use the resource loader above (and lets assume
>> it works I havent tested it lol)
>> $rowClass = 'Model_DbRow_User'
>>
>> Hope that helps some.
>>
>>
>> remataklan wrote:
>>>
>>> I have two files as you mentioned Users.php and User.php
>>> If I instantiate User class (like in a line above other), it does so
>>> without any problem.
>>> However if I dont and let the job to Users.php to do so. It looks up in
>>> "Model" directory instead of "Models" and of course cant find the
>>> correct file.
>>>
>>> I'm asking this because you dont instantiate row classes separately in
>>> your code. (and I dont think you should.)
>>>
>>>
>>> yidarmy2009 wrote:
>>>>
>>>> You need to have a file models/dbtable/User.php:
>>>>
>>>> class Model_DbTable_User extends Zend_Db_Row_Abstract
>>>> {
>>>>
>>>> }
>>>>
>>>> Just to clarify for each table model you need two files - 1 that
>>>> represents the table object and one that represents the row object (ie
>>>> where you have a single result object).
>>>>
>>>> In the case of users the logical way to name this is Users.php for the
>>>> table and User.php for the row.
>>>>
>>>> This zend_db_table was the way I initially learned to implement sql
>>>> tables in the zend framework from reading the only two reasonable books
>>>> I could find on the subject: Zend Framework In Action and Practical Web
>>>> Applications 2.0 with PHP. Both books used this method and it works so
>>>> I've never given it a second thought really.
>>>>
>>>> I like the way the database is manipulated like an object in the
>>>> application. The only downside I've really found with it is that
>>>> sometimes just writing an SQL query seems alot simpler than doing all
>>>> these models :P
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> remataklan wrote:
>>>>>
>>>>> 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-tp23445168p23490288.html
Sent from the Zend Framework mailing list archive at Nabble.com.