Advertisement
Guest User

Untitled

a guest
Apr 19th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. public function savePostTopics($postId, $topics)
  2. {
  3. // Explode the topics by comma, so we have an array to run through
  4. $topics = explode(',', $topics);
  5. // Array for collecting all the data
  6. $collection = array();
  7.  
  8. foreach($topics as $topic)
  9. {
  10. // Trim it so remove unwanted white spaces in the beginning and the end.
  11. $topic = trim($topic);
  12.  
  13. // Make it all lowercase for consistency of tag names
  14. $topic = strtolower($topic);
  15.  
  16. // Check if we already have a topic like this
  17. $controlFind = $this->find(
  18. 'first',
  19. array(
  20. 'conditions' => array(
  21. 'title' => $topic
  22. ),
  23. 'recursive' => -1
  24. )
  25. );
  26.  
  27. // No record found
  28. if(!$controlFind)
  29. {
  30. $this->create();
  31. if(
  32. !$this->save(
  33. array(
  34. 'title' => $topic
  35. )
  36. )
  37. )
  38. {
  39. // If only one saving fails we stop the whole loop and method.
  40. return false;
  41. }
  42. else
  43. {
  44. $temp = array(
  45. 'TopicPost' => array(
  46. 'topic_id' => $this->id,
  47. 'post_id' => $postId
  48. )
  49. );
  50. }
  51. }
  52. else
  53. {
  54. $temp = array(
  55. 'TopicPost' => array(
  56. 'topic_id' => $controlFind['Topic']['id'],
  57. 'post_id' => $postId
  58. )
  59. );
  60. }
  61.  
  62. $collection[] = $temp;
  63. }
  64.  
  65. return $this->TopicPost->saveMany($collection, array('validate' => false));
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement