TP5: create a personal blog, add a function, and tp5 blog

Source: Internet
Author: User
Tags form post

TP5: create a personal blog, add a function, and tp5 blog

Production record of the article adding function (Controller part) (use one-to-one and many-to-many associations)

Style is probably like this, the amazeUi template and H-ui.admin open source background template together to do a page, although I feel that the page is ugly...

 

Because TP5 has many ways to operate databases, it is constantly reviewing and exploring in use, hoping to record and skillfully use one of them.

Front-end form post data has

Title, cat_id, tags (TAG string set separated by commas ),

Img_path, thumb_path,

Subtitle, editorValue (content)

The table to be input has

Article, article_content (associated table), tag_relation (intermediate table), tag

Table Fields

Article table

Article_content table

Tag_relation table

Tag table

 

Set several methods

// Upload the image and accept the post imgfile information. The returned array is the storage address and file suffix protected function uploadImg () {$ file = $ this-> request-> file ('img '); $ data ['text'] = strrchr ($ file-> getInfo () ['name'], '. '); if ($ file) {$ fileInfo = $ file-> move (ROOT_PATH. 'public '. DS. 'uploads'); if ($ fileInfo) {$ data ['img _ path'] = DS. "uploads ". DS. $ fileInfo-> getSaveName ();} else {$ this-> error ($ fileInfo-> getError () ;}} return $ data ;}
1 // generate a thumbnail, input the source image path and file suffix, and output the thumbnail Path 2 protected function createThumb ($ img_path, $ ext) 3 {4 $ thumb_path = dirname ($ img_path ). DS. md5 (microtime ()). $ ext; 5 $ image = \ think \ Image: open ('. '. $ img_path); 6 $ image-> thumb (364,227)-> save ('. '. $ thumb_path); 7 return $ thumb_path; 8}
1 // enter the Document Object, tags string, and check whether the tag table has a tag with the same name. 2 // If yes, only the link is added to the intermediate table. If no tag exists, the tag is added, and the intermediate table adds the relationship 3 protected function tagSave ($ article, $ tags) 4 {5 $ new_tag = explode (',', $ tags ); 6 foreach ($ new_tag as $ vo) {7 $ check_tag = Tag: where ('name', $ vo)-> count (); 8 // This Tag exists, then, add the join operation for the tag table 9 if ($ check_tag = 0) {10 $ status = $ article-> tag () -> save (['name' => $ vo])? False: true; 11 // If the Tag does not exist, enter the tag object in the Add Association operation. The Tag object identifies the IDs on both sides and adds this field to the intermediate table, if you want to write your own SQL statements, you may need to explain it.) 12} else {13 $ status = $ article-> tag ()-> save ($ check_tag )? False: true; 14}
// Determine whether the operation is successful 15 if ($ status) {16 return $ article-> tag ()-> getError (); 17} 18} 19}

 

Then, how to add this article

1 // Add background processing 2 public function articleAddExecu () 3 {4 $ data = $ this-> request-> param (); 5 // Image Upload processing 6 $ file = $ this-> uploadImg (); 7 $ data ['img _ path'] = $ file ['img _ path']; 8 // generate a thumbnail 9 $ data ['thumb _ path'] = $ this-> createThumb ($ file ['img _ path'], $ file ['ext ']); 10 // user information 11 $ data ['user _ id'] = $ this-> request-> session ('user _ id '); 13 14 // save article15 $ article = new ArticleModel ($ data); 16 $ article-> allowField (true)-> save (); 17 // save article_content18 $ article-> articleContent ()-> save (['content' => $ data ['editorvalue']); 19 // store the tag table 20 $ this-> tagSave ($ article, $ data ['tags']); 21}

 

When using the associated model, several tables do not need to be manually concatenated with SQL, and the entire model architecture is quite clear. Really good.

I am not very familiar with the framework, and I have made some learning records, which is very helpful.

There are still many shortcomings. If you find any mistakes, I hope you can correct them. I am very grateful to you.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.