2011-08-05 8 views
0

私はポストモデル(MySQLのテーブルを)持っている、との構造のようなものです:CakePHPサブクエスト、どのように?

id, parent, user_id, title, desc, created, modified 

会話の単一のスレッドがpost.parentが元にpost_idと一致した後続のすべての記事に続いて、ポスト、から構成されています。

サンプルuser_32とuser_40間の会話:内部クエリに続いて、私は単純に外部クエリを行うプレーンなPHPで

110, 0, 32, thread_title, sample_description_here, created_time, modified_time 
121, 110, 40, comment_title, sample_comment_here, created_time, modified_time 
130, 110, 32, comment_title, sample_comment_here, created_time, modified_time 
140, 110, 32, comment_title, sample_comment_here, created_time, modified_time 
166, 110, 40, comment_title, sample_comment_here, created_time, modified_time 
290, 110, 32, comment_title, sample_comment_here, created_time, modified_time 

、その後は画面に会話をエコーが、どのようにあなたは、CakePHPでこれを実現します??

質問:私は(上記参照)post_id_110、post.parentの== 110は、CREATED_TIMEのDESC順後続のすべての記事が続いからなる単会話を表示するには、CakePHPの中にクエリを作成するにはどうすればよい

。 ??ポストモデルで

答えて

1

で動作します:

var $hasMany = array(
    'Children'=>array(
    'className' => 'Post', 
    'foreignKey' => 'parent_id' 
    ) 
); 

(DBでは 'parent'を 'parent_id'に変更します。その後の記事では、コントローラ:

$this->Post->find('first',array(
    'conditions'=>array(...), 
    'contain'=>array('Children') 
)); 

そうそう、及び収容可能を使用しています。あなたはその目的のためにそれが本当に必要なわけではありませんが、それはfind()を明確にし、後で間違いなく役立ちます。

+0

クールで、どのように私はビューでそれを描くのですか?私は現在foreachループを持っていますが、私は上記の変更を行ってから働きません。ありがとう –

+0

ちょうどその結果の配列を見るためにfind()の出力を表示し、それをループすることができます。 –

1

:その後、

var $belongsTo = array(
    'ParentPost' => array(
     'className' => 'Post', 
     'foreignKey' => 'parent_id', 
    ), 
) 

とところで、他のすべての仲間のモデルとして

を使用する - PARENT_IDは親よりcakeishで、Postモデルで足場

+0

こんにちは、 "アソシエイトモデル"とはどういう意味ですか?私のコントローラには$ this-> set( 'posts'、$ this-> Post-> find( 'all'、array( 'conditions' => $ conditions)))があります。だから私は今会話を表示するには?ありがとう –

+0

http://book.cakephp.org/view/1039/Associations-Linking-Models-Together –

関連する問題