Thanks for the explanations, i am starting to get used to how cake works. :)
And about the solution, i am so ashamed ...it was one line of code i forgot to delete from the user login function. This line was doing an update in the users table, which affected the join table too. I haven't seen it because in the login function i was doing a redirect, and the update query wasn't visible. Maybe this helps other newbies ;) Many thanks four your patience, David On May 20, 11:26 am, "David C. Zentgraf" <[EMAIL PROTECTED]> wrote: > On 20 May 2008, at 16:48, oana wrote: > > > $this->User->Conversation->findAll() works, but retieves all the > > conversations, not just the ones for a certain user > > True. Even though it looks like you're working on a User's > Conversations, > $this->User->Conversation->findAll(); > and > $this->Conversation->findAll(); > are identical. > > All you're doing is execute the method findAll() of the Conversation > model. It doesn't matter wether you're doing it via $this- > >Conversation->findAll() or $this->A->Really->Long->Way->Around- > >Conversation->findAll(). The Conversation model doesn't care/know > how it's called. So yes, $this->User->Conversation->findAll() will > find ALL conversations, not just a specific users' ones. > > I know it's counter-intuitive and I wish Cake would be improved in > this regard. > Maybe in 2.0? > > Now why does it work the way it works? By convention Cake will > automatically (try to) include the "User" model in your > "UserController", giving you the possibility to access $this->User. > The Conversation model is not automatically included, though you could > include it manually and then also get access to $this->Conversation. > The User model though happens to be linked to the Conversation model > through associations, so from the User model you can always access the > Conversation model. So in this case to use the Conversation model you > "go through" the User model. > > Anyway, to find the conversations of a specific user, you do a find() > on the User model: $this->User->find($conditions); > This will give you the user record AND records of all associated > models, including Conversations. > > > $this->User->ConversationsUser->findAllByUserId($userId) deletes the > > entries in the ConversationsUser table. > > I don't know why it deletes entries, but you shouldn't have a > ConversationsUser model in the first place! That should just be a > table that helps you link Users to Conversations, but it should never > be accessed directly. > > > On May 19, 4:26 pm, oana <[EMAIL PROTECTED]> wrote: > >> I wrote almost everything again, because it was getting too messy, > >> and > >> now it works. (though the changes i've made don't concern any logic > >> that may delete entries) > > >> Thank you very much for helping. > > >> On May 19, 11:09 am, David Christopher Zentgraf <[EMAIL PROTECTED]> > >> wrote: > > >>> Well, nothing's being deleted in those queries, only SELECTed. > >>> Of the top of my head I'd say there are two possibilities: > > >>> 1) Some really weird problem with your database engine that causes > >>> it > >>> to forget selected data. * > >>> 2) There's some logic in your application that deletes entries which > >>> is accidentally triggered. > >>> *unlikely > > >>> What you usually do to select related model data is just this: > >>> - You define your two models very simply, as > >>> inhttp://book.cakephp.org/view/66/models#introduction-67 > >>> - You set up associations between your two models as > >>> inhttp://book.cakephp.org/view/66/models#associations-78 > >>> - In your controller you do something like $this->User->find(), > >>> which > >>> returns an array like: > > >>> Array > >>> ( > >>> [User] => Array > >>> ( > >>> ... > >>> ) > >>> [Conversation] => Array > >>> ( > >>> ... > >>> ) > >>> ) > > >>> If you want to search for a specific entry in "Conversation" you'd > >>> do > >>> something like this: > >>> $this->User->Conversation->find(array('topic' => 'How to bake > >>> cookies')); > > >>> If your code is anymore complicated than that you're probably not > >>> doing it right. > >>> If you paste some more of your code (relevant controller actions, > >>> model definitions etc.) into the bin (http://bin.cakephp.org/) we > >>> might be able to tell where the problem is. > > >>> On 19 May 2008, at 16:35, oana wrote: > > >>>> These are the queries related concerning users, conversations, > >>>> and the > >>>> join table. > > >>>> If it isn't too much trouble, maybe you can point me to a complete > >>>> piece of code for the users cotroller, to select a certain user's > >>>> conversations. Because i have tried several, and i think this is my > >>>> problem. > > >>>> Thanks for helping, > >>>> Oana > > >>>> SELECT `Conversation`.`id`, `Conversation`.`availability`, > >>>> `Conversation`.`created`, `Conversation`.`modified`, > >>>> `Conversation`.`description`, `Conversation`.`title`, > >>>> `Conversation`.`postscount`, `Conversation`.`ideascount`, > >>>> `ConversationsUser`.`conversation_id`, > >>>> `ConversationsUser`.`user_id` > >>>> FROM `conversations` AS `Conversation` JOIN `conversations_users` > >>>> AS > >>>> `ConversationsUser` ON (`ConversationsUser`.`user_id` IN (1) AND > >>>> `ConversationsUser`.`conversation_id` = `Conversation`.`id`) > > >>>> SELECT COUNT(*) AS `count` FROM `conversations_users` AS > >>>> `ConversationsUser` LEFT JOIN `conversations` AS `Conversation` ON > >>>> (`ConversationsUser`.`conversation_id` = `Conversation`.`id`) LEFT > >>>> JOIN `users` AS `User` ON (`ConversationsUser`.`user_id` = > >>>> `User`.`id`) WHERE `User`.`id` = 1 > > >>>> SELECT `ConversationsUser`.`conversation_id`, > >>>> `ConversationsUser`.`user_id`, `Conversation`.`id`, > >>>> `Conversation`.`availability`, `Conversation`.`created`, > >>>> `Conversation`.`modified`, `Conversation`.`description`, > >>>> `Conversation`.`title`, `Conversation`.`postscount`, > >>>> `Conversation`.`ideascount`, `User`.`id`, `User`.`username`, > >>>> `User`.`email`, `User`.`password`, `User`.`group`, > >>>> `User`.`first_name`, `User`.`last_name` FROM > >>>> `conversations_users` AS > >>>> `ConversationsUser` LEFT JOIN `conversations` AS `Conversation` ON > >>>> (`ConversationsUser`.`conversation_id` = `Conversation`.`id`) LEFT > >>>> JOIN `users` AS `User` ON (`ConversationsUser`.`user_id` = > >>>> `User`.`id`) WHERE `User`.`id` = 1 LIMIT 20 > > >>>> On May 18, 11:53 am, David Christopher Zentgraf <[EMAIL PROTECTED]> > >>>> wrote: > >>>>> Can you post the SQL queries generated by Cake? > >>>>> Sounds really weird. > > >>>>> On 18 May 2008, at 16:04, oana wrote: > > >>>>>> Hi Keith, > > >>>>>> Your summary is very accurate, that is exactly what's happening. > > >>>>>> This is how i select the conversations for an user, in the user > >>>>>> controller: > > >>>>>> $this->set('conversations',$this->paginate('ConversationsUser', > >>>>>> array('User.id' => $userId))); > > >>>>>> Thank you for replying. > > >>>>>> On May 18, 6:48 am, Keith <[EMAIL PROTECTED]> wrote: > >>>>>>> Hi Oana, > > >>>>>>> Can you post some code? > > >>>>>>> What do you mean by "extract?" > > >>>>>>> It sounds like you've got your HABTM working properly, but > >>>>>>> when you > >>>>>>> try to create a view that shows conversations for a specific > >>>>>>> user > >>>>>>> the > >>>>>>> join table rows associated with that user are deleted. Is > >>>>>>> this an > >>>>>>> accurate summary of your problem? > > >>>>>>> On May 17, 4:34 am, oana <[EMAIL PROTECTED]> wrote: > > >>>>>>>> I know it has been posted a lor about this topic...but i can't > >>>>>>>> figure > >>>>>>>> it out. Maybe someone with some experience in cake may help me, > >>>>>>>> because i am a newbie. > > >>>>>>>> I have 2 classes, Users and Conversations, between them it's > >>>>>>>> habtm > >>>>>>>> relationship, and i have the join table ConversationsUser. > > >>>>>>>> In the Users controller i have a function which extracts all > >>>>>>>> the > >>>>>>>> conversations of an user give by it's id. Whenever i try to do > >>>>>>>> this, > >>>>>>>> the fields in the join table are deleted. > > >>>>>>>> Can anyone please help me? I didn't post this right away, i > >>>>>>>> tried a > >>>>>>>> lot to make it work, but i think it's something big i am > >>>>>>>> missing. > > >>>>>>>> Thanks, > >>>>>>>> Oana --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---