Guest User

Untitled

a guest
Jul 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. How to delete all the tags in edit post of cakephp framwork?
  2. <?php
  3. public $displayField = 'title';
  4.  
  5. public $hasAndBelongsToMany = array(
  6. 'Tag' => array(
  7. 'className' => 'Tag',
  8. 'joinTable' => 'posts_tags',
  9. 'foreignKey' => 'post_id',
  10. 'associationForeignKey' => 'tag_id',
  11. 'unique' => 'keepExisting',
  12. 'conditions' => '',
  13. 'fields' => '',
  14. 'order' => '',
  15. 'limit' => '',
  16. 'offset' => '',
  17. 'finderQuery' => '',
  18. 'deleteQuery' => '',
  19. 'insertQuery' => '',
  20. )
  21. );
  22.  
  23. <?php
  24. public $displayField = 'name';
  25.  
  26. /**
  27. * hasAndBelongsToMany associations
  28. *
  29. * @var array
  30. */
  31. public $hasAndBelongsToMany = array(
  32. 'Post' => array(
  33. 'className' => 'Post',
  34. 'joinTable' => 'posts_tags',
  35. 'foreignKey' => 'tag_id',
  36. 'associationForeignKey' => 'post_id',
  37. 'unique' => 'keepExisting',
  38. 'conditions' => '',
  39. 'fields' => '',
  40. 'order' => '',
  41. 'limit' => '',
  42. 'offset' => '',
  43. 'finderQuery' => '',
  44. 'deleteQuery' => '',
  45. 'insertQuery' => ''
  46. )
  47. );
  48.  
  49. <?php
  50. public function admin_edit($id = null) {
  51. $this->Post->id = $id;
  52. if (!$this->Post->exists()) {
  53. throw new NotFoundException(__('Invalid post'));
  54. }
  55. if ($this->request->is('post') || $this->request->is('put')) {
  56.  
  57. $tags = explode(',', $this->request->data['Post']['tags']);
  58. foreach ($tags as $_tag) {
  59. $_tag = strtolower(trim($_tag));
  60. if ($_tag) {
  61. $this->Post->Tag->recursive = -1;
  62. $tag = $this->Post->Tag->findByName($_tag);
  63. if (!$tag) {
  64. $this->Post->Tag->create();
  65. $tag = $this->Post->Tag->save(array('name' => $_tag, 'slug' => $_tag));
  66. $tag['Tag']['id'] = $this->Post->Tag->id;
  67. if (!$tag) {
  68. $this->_flash(__(sprintf('The Tag %s could not be saved.', $_tag), true), 'success');
  69. }
  70. }
  71. if ($tag) {
  72. $this->request->data['Tag']['Tag'][$tag['Tag']['id']] = $tag['Tag']['id'];
  73. }
  74. }
  75. }
  76.  
  77. if ($this->Post->save($this->request->data)) {
  78.  
  79. $this->Session->setFlash(__('The post has been saved'));
  80. $this->redirect(array('action' => 'index'));
  81. } else {
  82. $this->Session->setFlash(__('The post could not be saved. Please, try again.'));
  83. }
  84. } else {
  85. if (empty($this->request->data)) {
  86. $this->request->data = $this->Post->read(null, $id);
  87. $tags = array();
  88. if (isset($this->request->data['Tag']) && !empty($this->request->data['Tag'])) {
  89. foreach ($this->request->data['Tag'] as $tag) {
  90. $tags[] = $tag['name'];
  91. }
  92. $this->request->data['Post']['tags'] = implode(', ', $tags);
  93. }
  94. }
  95. }
  96. }
  97.  
  98. echo $this->Form->create('Post');
  99. echo __('Admin Edit Post');
  100. echo $this->Form->input('id');
  101. echo $this->Form->input('user_id');
  102. echo $this->Form->input('title');
  103. echo $this->Form->input('slug');
  104. echo $this->Form->input('content');
  105. echo $this->Form->input('excerpt');
  106. echo $this->Form->input('status');
  107. echo $this->Form->input('comment_status');
  108. echo $this->Form->input('comment_count');
  109. echo $this->Form->input('type');
  110. echo $this->Form->input('Category');
  111. echo $this->Form->input('Post.tags');
  112. echo $this->Form->end(__('Submit'));
  113.  
  114. $conditions = array('Tag.id' => 1234);
  115. $this->Post->Tag->deleteAll($conditions, false);
Add Comment
Please, Sign In to add comment