2016-03-22 21 views
0

Ich versuche, eine Abfrage mit CakePHP-Modell verbindlich machen. Ein unerwartetes Ergebnis erhalten Aber wann immer ich die Abfrage in phpmyadmin ausführen, funktioniert es gut. Immer diese FehlermeldungCakephp Suche Abfrage funktioniert nicht

Column not found: 1054 Unknown column 'Comment.post_id' in 'field list' 

Kann nicht herausfinden, was falsch ist

Posts

$posts = $this->Post->find('all', array(
     'conditions' => array(
      'Post.is_deleted' => 0, 
      'Post.is_active' => 1, 
     ), 
     'fields' => array(
      'Post.post_id,Post.post_title,Post.post_body,User.username,Post.added_time', 
      'Product.product_title','COUNT(Comment.post_id) AS total_comment' 
     ), 
     'GROUP'=>'Post.post_id', 
     'order' => 'added_time DESC', 
    )); 

Beitrag Modell

public $belongsTo = array(
    'User' => array(
     'className' => 'User', 
     'foreign_key' => 'post_id' 
    ), 
    'Product' => array(
     'className' => 'Product', 
     'foreign_key' => 'post_id' 
    ) 
); 

public $hasMany = array(
    'Comment'=>array(
     'className'=>'Comment', 
     'foreign_key'=>'post_id' 
    ) 
); 

Kommentar Modell

public $belongsTo = array(
    'Post'=>array(
     'className'=>'Post', 
     'foreign_key'=>'post_id' 
    ) 
); 

Meine erwartete Abfrage

SELECT posts.`post_title`, users.username,products.product_title, posts.post_body, COUNT(comments.post_id) as total_comments from posts LEFT JOIN comments on comments.post_id = posts.post_id LEFT join users on users.user_id = posts.user_id LEFT JOIN products on products.product_id = posts.product_id WHERE posts.is_active = 1 and posts.is_deleted = 0 GROUP BY posts.post_id 

Antwort

1

Versuche durch das rekursive Level 1 oder 2 Einstellung

$this->Post->find('all', array(
     'conditions' => array(
      'Post.is_deleted' => 0, 
      'Post.is_active' => 1, 
     ), 
     'fields' => array(
      'Post.post_id,Post.post_title,Post.post_body,User.username,Post.added_time', 
      'Product.product_title','COUNT(Comment.post_id) AS total_comment' 
     ), 
     'GROUP'=>'Post.post_id', 
     'order' => 'added_time DESC', 
'recursive'=>1 
    )); 
+0

Kein Glück noch die gleichen Fehler. Ich denke, es gibt einen Fehler in der Beziehung – Amir