cakePHP - belongsTo - conditions - Kommentaranzahl

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • cakePHP - belongsTo - conditions - Kommentaranzahl

    Hallo,

    die aktuelle Query sieht so aus

    Quellcode

    1. SELECT `Post`.`id`, `Post`.`title`, `Post`.`inhalt`, `Post`.`created`, `Post`.`modified`, `Post`.`category_id`, `Comment`.`id`, `Comment`.`comment`, `Comment`.`created`, `Comment`.`creator_id`, `Comment`.`modified`, `Comment`.`modifier_id`, `Comment`.`post_id`, `Category`.`id`, `Category`.`name` FROM `cake_posts` AS `Post` LEFT JOIN `cake_comments` AS `Comment` ON (`Post`.`id` = 'Comment.post_id') LEFT JOIN `cake_categories` AS `Category` ON (`Post`.`category_id` = `Category`.`id`) WHERE 1 = 1


    und die Ausgabe sieht so aus

    Quellcode

    1. id title inhalt created modified category_id id comment created creator_id modified modifier_id post_id id name
    2. 1 878 This is the post body. admin test 2011-09-26 12:20:35 2011-10-04 11:18:20 2 NULL NULL NULL NULL NULL NULL NULL 2 cat 2
    3. 2 Test12 Blubb this ondsf three23 tst 2011-09-26 12:20:35 2011-10-04 11:18:15 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1
    4. 3 123 3123123asd 2011-09-27 16:44:03 2011-10-04 11:18:09 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1
    5. 4 tst test 2011-09-28 18:13:50 2011-10-04 11:20:05 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1
    6. 5 esedt sdfsdf 2011-10-04 13:15:31 2011-10-04 13:15:31 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1
    7. 6 esedt sdfsdf 2011-10-04 13:17:03 2011-10-04 13:17:03 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1
    8. 7 esedt sdfsdf 2011-10-04 13:19:29 2011-10-04 13:19:29 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1
    9. 8 asdsad asdasd 2011-10-04 16:12:28 2011-10-04 16:12:28 1 NULL NULL NULL NULL NULL NULL NULL 1 cat 1


    Das ganze macht das PostModell


    Quellcode

    1. var $belongsTo = array(
    2. 'Category',
    3. 'Comment' => array(
    4. 'foreignKey' => false,'conditions' => array('Post.id' => 'Comment.post_id'))
    5. );


    Was ich genau will:

    aus

    LEFT JOIN `cake_comments` AS `Comment` ON (`Post`.`id` = 'Comment.post_id')


    soll

    LEFT JOIN `cake_comments` AS `Comment` ON (`Post`.`id` = `Comment`.`post_id'`)


    werden.

    Ich will eigentlich nur die Kommentaranzahl ausgeben lassen und zwar in dem Array in dem die anderen Sachen auch stehen.

    Quellcode

    1. $eintraege = $this->Post->find('all');
    2. $this->set("posts",$eintraege);


    Quellcode

    1. [0] => Array
    2. (
    3. [Post] => Array
    4. (
    5. [id] => 1
    6. [title] => 878
    7. [inhalt] => This is the post body. admin test
    8. [created] => 2011-09-26 12:20:35
    9. [modified] => 2011-10-04 11:18:20
    10. [category_id] => 2
    11. )
    12. [Comment] => Array
    13. (
    14. [id] =>
    15. [comment] =>
    16. [created] =>
    17. [creator_id] =>
    18. [modified] =>
    19. [modifier_id] =>
    20. [post_id] =>
    21. )
    22. [Category] => Array
    23. (
    24. [id] => 2
    25. [name] => cat 2
    26. )
    27. )
    Alles anzeigen



    Hoffe es ist ausführlich genug um mein Problem zu schildern.

    gruß



    EDIT://

    Sowas wäre optimal =>


    Quellcode

    1. SELECT `Post`.`id` , `Post`.`title` , `Post`.`inhalt` , `Post`.`created` , `Post`.`modified` , `Post`.`category_id` , COUNT( `Comment`.`post_id` ) AS CommentCount, `Category`.`id` , `Category`.`name`
    2. FROM `cake_posts` AS `Post`
    3. LEFT JOIN `cake_comments` AS `Comment` ON ( `Post`.`id` = `Comment`.`post_id` )
    4. LEFT JOIN `cake_categories` AS `Category` ON ( `Post`.`category_id` = `Category`.`id` )
    5. WHERE `Post`.`id` =1




    Quellcode

    1. id title inhalt created modified category_id CommentCount id name
    2. 1 878 This is the post body. admin test 2011-09-26 12:20:35 2011-10-04 11:18:20 2 9 2 cat 2
  • Hi,

    ich denke mal, das dort keine Ergebnisse kommen liegt daran, dass du den foreignKey auf false stellst und die Beziehung über die Condition löst.
    Ich habe keine Ahnung von CakePHP, aber der foreignKey sollte schon für Beziehungen und die Condition für Bedingungen genutzt werden.

    Quellcode

    1. var $belongsTo = array(
    2. 'Category',
    3. 'Comment' => array(
    4. 'className' => 'Comment'
    5. 'foreignKey' => 'post_id'
    6. )
    7. );



    Alternativ könntest du auch versuchen, das foreignKey nicht explizit auf false zu setzen.

    Quellcode

    1. var $belongsTo = array(
    2. 'Category',
    3. 'Comment' => array(
    4. 'conditions' => array('Post.id' => 'Comment.post_id'))
    5. );