Advertisement
kwixson

Comments MCV

May 8th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. ==============MODEL=================
  2.  
  3. <?php
  4. App::uses('AppModel', 'Model');
  5. /**
  6. * Comment Model
  7. *
  8. * @property Post $Post
  9. * @property User $User
  10. */
  11. class Comment extends AppModel {
  12. /**
  13. * Display field
  14. *
  15. * @var string
  16. */
  17. public $displayField = 'name';
  18. /**
  19. * Validation rules
  20. *
  21. * @var array
  22. */
  23. public $validate = array(
  24. 'name' => array(
  25. 'maxlength' => array(
  26. 'rule' => array('maxlength', 50),
  27. 'message' => 'The title can not be empty or longer than 50 characters.',
  28. 'allowEmpty' => false,
  29. //'required' => false,
  30. //'last' => false, // Stop validation after this rule
  31. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  32. ),
  33. ),
  34. 'content' => array(
  35. 'notempty' => array(
  36. 'rule' => array('notempty'),
  37. 'message' => 'Your comment has no text.',
  38. 'allowEmpty' => false,
  39. //'required' => false,
  40. //'last' => false, // Stop validation after this rule
  41. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  42. ),
  43. ),
  44. );
  45.  
  46. //The Associations below have been created with all possible keys, those that are not needed can be removed
  47.  
  48. /**
  49. * belongsTo associations
  50. *
  51. * @var array
  52. */
  53. public $belongsTo = array(
  54. 'Post' => array(
  55. 'className' => 'Post',
  56. 'foreignKey' => 'post_id',
  57. 'conditions' => '',
  58. 'fields' => '',
  59. 'order' => ''
  60. ),
  61. 'User' => array(
  62. 'className' => 'User',
  63. 'foreignKey' => 'user_id',
  64. 'conditions' => '',
  65. 'fields' => '',
  66. 'order' => ''
  67. )
  68. );
  69. }
  70.  
  71.  
  72. ================CONTROLLER======================
  73.  
  74. <?php
  75. App::uses('AppController', 'Controller');
  76. /**
  77. * Comments Controller
  78. *
  79. * @property Comment $Comment
  80. */
  81. class CommentsController extends AppController {
  82.  
  83.  
  84. /**
  85. * index method
  86. *
  87. * @return void
  88. */
  89. public function index() {
  90. $this->Comment->recursive = 0;
  91. $this->set('comments', $this->paginate());
  92. }
  93.  
  94. /**
  95. * view method
  96. *
  97. * @param string $id
  98. * @return void
  99. */
  100. public function view($id = null) {
  101. $this->Comment->id = $id;
  102. if (!$this->Comment->exists()) {
  103. throw new NotFoundException(__('Invalid comment'));
  104. }
  105. $this->set('comment', $this->Comment->read(null, $id));
  106. }
  107.  
  108. /**
  109. * add method
  110. *
  111. * @return void
  112. */
  113. public function add() {
  114. if ($this->request->is('post')) {
  115. $this->Comment->create();
  116. if ($this->Comment->save($this->request->data)) {
  117. $this->Session->setFlash(__('The comment has been saved'));
  118. $this->redirect(array('action' => 'index'));
  119. } else {
  120. $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
  121. }
  122. }
  123. $posts = $this->Comment->Post->find('list');
  124. $this->set(compact('posts'));
  125. }
  126.  
  127. /**
  128. * edit method
  129. *
  130. * @param string $id
  131. * @return void
  132. */
  133. public function edit($id = null) {
  134. $this->Comment->id = $id;
  135. if (!$this->Comment->exists()) {
  136. throw new NotFoundException(__('Invalid comment'));
  137. }
  138. if ($this->request->is('post') || $this->request->is('put')) {
  139. if ($this->Comment->save($this->request->data)) {
  140. $this->Session->setFlash(__('The comment has been saved'));
  141. $this->redirect(array('action' => 'index'));
  142. } else {
  143. $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
  144. }
  145. } else {
  146. $this->request->data = $this->Comment->read(null, $id);
  147. }
  148. $posts = $this->Comment->Post->find('list');
  149. $this->set(compact('posts'));
  150. }
  151.  
  152. /**
  153. * delete method
  154. *
  155. * @param string $id
  156. * @return void
  157. */
  158. public function delete($id = null) {
  159. if (!$this->request->is('post')) {
  160. throw new MethodNotAllowedException();
  161. }
  162. $this->Comment->id = $id;
  163. if (!$this->Comment->exists()) {
  164. throw new NotFoundException(__('Invalid comment'));
  165. }
  166. if ($this->Comment->delete()) {
  167. $this->Session->setFlash(__('Comment deleted'));
  168. $this->redirect(array('action' => 'index'));
  169. }
  170. $this->Session->setFlash(__('Comment was not deleted'));
  171. $this->redirect(array('action' => 'index'));
  172. }
  173. }
  174.  
  175.  
  176.  
  177. ===============VIEW(ADD)======================
  178.  
  179.  
  180. <div class="comments form">
  181. <?php echo $this->Form->create('Comment');?>
  182. <fieldset>
  183. <legend><?php echo __('Add Comment'); ?></legend>
  184. <?php
  185. echo $this->Form->input('post_id');
  186. echo $this->Form->input('name');
  187. echo $this->Form->input('content');
  188. echo $this->Form->input('user_id');
  189. ?>
  190. </fieldset>
  191. <?php echo $this->Form->end(__('Submit'));?>
  192. </div>
  193. <div class="actions">
  194. <h3><?php echo __('Actions'); ?></h3>
  195. <ul>
  196. <li><?php echo $this->Html->link(__('List Comments'), array('action' => 'index'));?></li>
  197. <li><?php echo $this->Html->link(__('List Posts'), array('controller' => 'posts', 'action' => 'index')); ?> </li>
  198. <li><?php echo $this->Html->link(__('New Post'), array('controller' => 'posts', 'action' => 'add')); ?> </li>
  199. </ul>
  200. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement