Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==============MODEL=================
- <?php
- App::uses('AppModel', 'Model');
- /**
- * Comment Model
- *
- * @property Post $Post
- * @property User $User
- */
- class Comment extends AppModel {
- /**
- * Display field
- *
- * @var string
- */
- public $displayField = 'name';
- /**
- * Validation rules
- *
- * @var array
- */
- public $validate = array(
- 'name' => array(
- 'maxlength' => array(
- 'rule' => array('maxlength', 50),
- 'message' => 'The title can not be empty or longer than 50 characters.',
- 'allowEmpty' => false,
- //'required' => false,
- //'last' => false, // Stop validation after this rule
- //'on' => 'create', // Limit validation to 'create' or 'update' operations
- ),
- ),
- 'content' => array(
- 'notempty' => array(
- 'rule' => array('notempty'),
- 'message' => 'Your comment has no text.',
- 'allowEmpty' => false,
- //'required' => false,
- //'last' => false, // Stop validation after this rule
- //'on' => 'create', // Limit validation to 'create' or 'update' operations
- ),
- ),
- );
- //The Associations below have been created with all possible keys, those that are not needed can be removed
- /**
- * belongsTo associations
- *
- * @var array
- */
- public $belongsTo = array(
- 'Post' => array(
- 'className' => 'Post',
- 'foreignKey' => 'post_id',
- 'conditions' => '',
- 'fields' => '',
- 'order' => ''
- ),
- 'User' => array(
- 'className' => 'User',
- 'foreignKey' => 'user_id',
- 'conditions' => '',
- 'fields' => '',
- 'order' => ''
- )
- );
- }
- ================CONTROLLER======================
- <?php
- App::uses('AppController', 'Controller');
- /**
- * Comments Controller
- *
- * @property Comment $Comment
- */
- class CommentsController extends AppController {
- /**
- * index method
- *
- * @return void
- */
- public function index() {
- $this->Comment->recursive = 0;
- $this->set('comments', $this->paginate());
- }
- /**
- * view method
- *
- * @param string $id
- * @return void
- */
- public function view($id = null) {
- $this->Comment->id = $id;
- if (!$this->Comment->exists()) {
- throw new NotFoundException(__('Invalid comment'));
- }
- $this->set('comment', $this->Comment->read(null, $id));
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- if ($this->request->is('post')) {
- $this->Comment->create();
- if ($this->Comment->save($this->request->data)) {
- $this->Session->setFlash(__('The comment has been saved'));
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
- }
- }
- $posts = $this->Comment->Post->find('list');
- $this->set(compact('posts'));
- }
- /**
- * edit method
- *
- * @param string $id
- * @return void
- */
- public function edit($id = null) {
- $this->Comment->id = $id;
- if (!$this->Comment->exists()) {
- throw new NotFoundException(__('Invalid comment'));
- }
- if ($this->request->is('post') || $this->request->is('put')) {
- if ($this->Comment->save($this->request->data)) {
- $this->Session->setFlash(__('The comment has been saved'));
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
- }
- } else {
- $this->request->data = $this->Comment->read(null, $id);
- }
- $posts = $this->Comment->Post->find('list');
- $this->set(compact('posts'));
- }
- /**
- * delete method
- *
- * @param string $id
- * @return void
- */
- public function delete($id = null) {
- if (!$this->request->is('post')) {
- throw new MethodNotAllowedException();
- }
- $this->Comment->id = $id;
- if (!$this->Comment->exists()) {
- throw new NotFoundException(__('Invalid comment'));
- }
- if ($this->Comment->delete()) {
- $this->Session->setFlash(__('Comment deleted'));
- $this->redirect(array('action' => 'index'));
- }
- $this->Session->setFlash(__('Comment was not deleted'));
- $this->redirect(array('action' => 'index'));
- }
- }
- ===============VIEW(ADD)======================
- <div class="comments form">
- <?php echo $this->Form->create('Comment');?>
- <fieldset>
- <legend><?php echo __('Add Comment'); ?></legend>
- <?php
- echo $this->Form->input('post_id');
- echo $this->Form->input('name');
- echo $this->Form->input('content');
- echo $this->Form->input('user_id');
- ?>
- </fieldset>
- <?php echo $this->Form->end(__('Submit'));?>
- </div>
- <div class="actions">
- <h3><?php echo __('Actions'); ?></h3>
- <ul>
- <li><?php echo $this->Html->link(__('List Comments'), array('action' => 'index'));?></li>
- <li><?php echo $this->Html->link(__('List Posts'), array('controller' => 'posts', 'action' => 'index')); ?> </li>
- <li><?php echo $this->Html->link(__('New Post'), array('controller' => 'posts', 'action' => 'add')); ?> </li>
- </ul>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement