I'm making a simple Forum plugin to train with CakePHP.

It has 4 models: Forum, Topic, Post and User:

(You can pass by this part, I put it to explain better the problem)

<?php
class User extends ForumsAppModel {
        var $name = 'User';
        var $primaryKey = 'id';
}
?>

<?php
class Forum extends ForumsAppModel {
        var $name = 'Forum';
        var $useTable = 'forum_forums';
        var $primaryKey = 'forum_id';
}
?>

<?php
class Topic extends ForumsAppModel {

        var $name = 'Topic';
        var $useTable = 'forum_topics';
        var $primaryKey = 'topic_id';

        var $hasMany = array('Post' =>
                           array('className'  => 'Post',
                                 'conditions' => '',
                                 'order'      => '',
                                 'foreignKey' => 'topic_id'
                           )
                     );
}
?>

<?php
class Post extends ForumsAppModel {
        var $name = 'Posts';
        var $useTable = 'forum_posts';
        var $primaryKey = 'post_id';

        var $belongsTo = array('Usuario' =>
                           array('className'  => 'Usuario',
                                 'conditions' => '',
                                 'order'      => '',
                                 'foreignKey' => 'poster_id'
                           )
                     );
}
?>

When I user the Topic model to get the posts for the model, the
association Post-User doesn't work, generating these queries:

Ps: I changed the column names by an asterisk to make this post
cleaner.

1 - DESCRIBE `forum_topics`
2 - DESCRIBE `forum_posts`
3 - DESCRIBE `users`
4 - SELECT `Topic`.* FROM `forum_topics` AS `Topic` WHERE topic_id =
1
5 - SELECT `Post`.* FROM `forum_posts` AS `Post` WHERE
`Post`.`topic_id` IN (1)

... but it generates the correct query when I use the Post Model
directly:

1 - DESCRIBE `forum_posts`
2 - DESCRIBE `users`
3 - SELECT `Posts`.*, `User`.* FROM `forum_posts` AS `Posts` LEFT JOIN
`users` AS `User` ON (`Posts`.`poster_id` = `User`.`id`) WHERE 1 = 1

The correct queries should be:

1 - DESCRIBE `forum_topics`
2 - DESCRIBE `forum_posts`
3 - DESCRIBE `users`
4 - SELECT `Topic`.* FROM `forum_topics` AS `Topic` WHERE topic_id =
1
5 - SELECT `Posts`.*, `User`.* FROM `forum_posts` AS `Posts` LEFT JOIN
`users` AS `User` ON (`Posts`.`poster_id` = `User`.`id`)

I think the explanation for that behavior is: "Because Topics has many
Posts, Posts belongs to Topics and cannot belong to Users"

Is there a way to fix it, without using custom queries?? Thank you.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to