A learning website project is being prepared to participate in the national primary and secondary computer production activities, using the thinkphp framework
The project is to implement two-level nested comments, PID 0 for the first comment, otherwise for the two-level comment, two-level comment for the sub-comment for the first level comment.
Originally conceived as using the Thinkphp Association model, the Model code (interpretation please refer to the thinkphp related documentation) as follows:
The code is as follows |
Copy Code |
<?php
Class Articlecommentmodel extends Relationmodel {
Protected $_link = Array (
' User ' =>array (' Mapping_type ' => belongs_to,
' Foreign_key ' => ' uid ',
' Mapping_fields ' => ' Id,username,email ',
' Mapping_name ' => ' user ',
),
' Article ' => array (' Mapping_type ' => belongs_to,
' Foreign_key ' => ' aid ',
' Mapping_fields ' => ' id,res,gpid,gdid,sid,cid,desc,title ',
' Mapping_name ' => ' article ',
),
' Articlecomment ' =>array (
' Mapping_type ' =>has_many,
' Foreign_key ' => ' id ',
' Parent_key ' => ' pid ',
' Condition ' => ' pid!=0 ',
' Mapping_name ' => ' comments ',
),
' Articleapplaud ' =>array (
' Mapping_type ' =>has_many,
' Foreign_key ' => ' CID ',
' Parent_key ' => ' id ',
' Mapping_name ' => ' applaud ',
),
);
}
Www.111cn.net ?> |
The model can query the comments, as well as the relevant information, such as user information, review article Information, praise information.
However, because the association model query principle is based on the declaration of the foreign key to the corresponding table to query, if I do the comments page, one display 10 comments, one comment below a maximum of 10 child comments, to query the first level of comments, and then query users according to the records of information, article information, Word comments, etc., display a comment, There will be more than 10 inquiries.
Efficiency is really too low, personally, the association model is best suited to the database curd (the increase in the search) of the increase in the deletion.
So give up the idea that you can also use the view model. Then create a view model, the code (also refer to the thinkphp related documents) as follows:
The code is as follows |
Copy Code |
<?php Class Commentviewmodel extends ViewModel { Public $viewFields = Array ( ' Articlecomment ' => array (' ID ', ' uid ', ' aid ', ' pid ', ' Comment ', ' CTime '), ' User ' => Array (' username ', ' password ', ' email ', ' _on ' => ' user.id=articlecomment.uid ', ' _type ' => ' left '), ' UserInfo ' => Array (' name ', ' sex ', ' age ', ' mobile ', ' site ', ' address ', ' Zip ', ' intro ', ' _on ' => ') userinfo.uid=articl Ecomment.uid ', ' _type ' => ' left '), ); } ?> |
The above code has not yet written the appropriate information for the article query (if necessary to increase it). The query code is as follows:
code is as follows |
copy code |
$article = D (' Commentview '); Instantiate the comment view model $articleComments = $article-> where (Array (' aid ' => $aid, ' pid ' => 0)-> limit Select (); Query first-level comments Www.111cn.net foreach ($articleComments as $comment) { $comments [$comment [' id ']] = $comment; /Assign query-level comments to comments array } by id $commentID = Array_keys ($comments);//Remove the ID of the first level of comment and prepare the corresponding level two comment for the following query if (!empty $ Commentid) { foreach ($commentID as $id) { $sqlArray [] = ' pid= '. $id;//Generate query array of criteria for level two comments &nbs P;} $sql = implode (' or ', $sqlArray);//connect arrays, generate corresponding SQL conditional statements $articleComments = $article-> where ($sql) -> limit (a)-> select (); Query corresponds to level two comments foreach ($articleComments as $comment) { $comments [$comment [' pid ']][' comments '] [] = $comment; Arrange comments by Tree } $this-> assign (' comments ', $comments); |
The principle is well understood by the annotations.
1. First query out the PID for 0 comments (that is, the level of comments)
2. Record the comment ID of the first level comment and generate the SQL conditional clause
3. Use the generated conditional words to query, query out the previous query out of the first level of comments below the level two comments
4. The query out of the code in a tree-shaped array format processing, generate comments variable, assigned to the template display
This method only inquires two times database, and the efficiency is greatly improved.
If you want to do multi-level comments, you can add a level field, the corresponding processing, here no longer repeat, the reader can try.
Final effect: