Some people may throw rocks at me, but I find writing custom SQL queries not a bad solution for some tasks. Mind you! I <3 cakePHP and I tried doing the most complex finds using cakePHP conventions, with php arrays containing all my conditions... and I consistently run over my memory limit (128Mb allocated to PHP), so it wasn't very practical.
to Still: okay so you got your Users, Groups, and Posts (GroupPosts, but they're really Posts). User habtm Groups, Group has_many Post, Post belongs_to Group, User has_many Post, Post belongs_to User. (Above, you can and should discard the belongs_to relationships, as they are the same as has_many.) From this alone you can see that having Post.user_id field in Posts model (table) will give you a simple way of querying all posts by a user: $this->Post- >findAllByUserId($user_id); The Post then belongs to a Group by virtue of Group has_many Post, so you can sort posts by groups or whatnot. Look into Set::sort() for sorting. As has been said before, you named your groups_users table group_membership. That doesn't really matter as long as you know how to tell cakePHP that that is your join table. You do that in the model: var $hasAndBelongsToMany = array( 'Group' => array( 'className' => 'Group', 'joinTable' => 'groups_users, 'foreignKey' => 'user_id', 'associationForeignKey' => 'group_id', 'unique' => true ), And the shortcut: $this->Post->query("SELECT * FROM posts as Post LEFT JOIN..."); and then do your joins as required. I guarantee that it'll be 100 times faster than using cakePHP conventions, on large databases. Don't forget to use "as Post", otherwise there'll be a slight naming problem where instead of the model `Post` you'll have the model `posts`, which problem you can solve with Inflector::classify() but better to avoid it altogether. _V On May 18, 1:58 am, still <still...@gmail.com> wrote: > Ok Im not very familiar with google group too....I thought reply to > author means reply to this article.. > > below are the contents i wanted to post after you guys. > > > Thank you guys ~~ that's woking fine! and made me having deeper > > understanding with cake's model relationship conventions.. > > in fact im a Ruby on Rails user, and that's somehow made me choosed > > Cakephp for my php project. > > but .... you can see im trying to fit with cakephp's conventions..need > > some time. > > and these days i got confused with cakephp's pagination component > > again.... > > > So, as the former question's situation, how the pagination setup > > should be? : ( > > > Thank's a lot~~!!!!! > > AND !! Im having (found) another question is that HABTM association > won't work properly as i wished. > if i do > $this->User->GroupMembership->Group->GroupPost->find(somthing..............) > > the query generated by this method will left join USERS, GROUPS, > GROUPPOSTS three tables but no HABTM join table things. > and if you do > $this->User->GroupMembership->Group->find('all',array('conditions'=>array('User.id'=>$the_user_id))), > it > > will raise a SQL query error of > unknown column User.id in where clause. because it wont join users and > join table ,just seleted from groups table. > query as below: > > Query: SELECT `Group`.`id`, `Group`.`name`, `Group`.`desc`, > `Group`.`avatar`, `Group`.`created`, `Group`.`updated`, > `Group`.`status`, `Group`.`privacy` FROM `groups` AS `Group` WHERE > `User`.`id` = 4 ORDER BY `GroupMembership`.`created` ASC LIMIT 10 > > Paul, those you told me to try worked fine because just joining groups > and group_posts will be enough for finding all posts where groups in > id_array. > So..........Im confused again........... > > On May 17, 5:22 pm, WebbedIT <p...@webbedit.co.uk> wrote: > > > Totally agree, as soon as I am doing anything complex with HABTM I > > much prefer modelling my join table and giving it a proper name, was > > just advising the user who I assume is new to cake that it is easier > > to stick with conventions when using HABTM at the start as HABTM can > > easily get it's knickers in a twist. > > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others > > with their CakePHP related questions. > > > 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 > > cake-php+unsubscr...@googlegroups.com For more options, visit this group > > athttp://groups.google.com/group/cake-php?hl=en > > Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with > their CakePHP related questions. > > 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 > cake-php+unsubscr...@googlegroups.com For more options, visit this group > athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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 cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en