CakePHP Form

Controller:
Controller/PostsController.php
 <?php

class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    public function index(){
        $this->set('posts', $this->Post->find('all'));
    }

    public function view($id = null){
        if(!$id){
            throw new NotFoundException(__('Invalid post'));
        }
        $post = $this->Post->findById($id);

        if(!$post){
            throw new NotFoundException(__('Invalid post'));
        }
        $this->set('post', $post);
    }

    public function add(){
        $this->set('form_title', 'Add Post');

        $this->loadModel('category');
        $categories = $this->category->find('list');
        $this->set('categories', $categories);

        $this->edit();
    }
    public function edit($id = null){

        $this->set('form_title', 'Edit Post');


        $this->loadModel('category');
        $categories = $this->category->find('list');
        $this->set('categories', $categories);

        if($this->request->is('post') || $this->request->is('put') ){

            if($this->Post->save($this->request->data)){
                $this->redirect(array(
                    'controller' => 'posts',
                    'action' => 'index'
                ));
            }
        }

        if(!is_null($id)){
            $this->data  =   $this->Post->findById($id);
        }
        $this->render('form'); // Set the correct view
    }

    public function delete($id = null)
    {
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }

        if ($this->Post->delete($id)) {
            //$this->Session->setFlash(__('The user with id: %s has been deleted.', h($id))
            $this->Session->setFlash("Record has been successfully deleted !", 'default', array('class' => 'alert alert-success'));

            return $this->redirect(array('action' => 'index'));
        }
    }

}

View:
View/Posts/form.ctp
 
<?php

echo '<h1>'.$form_title.'<h1>';
echo $this->Form->create('Post');
echo $this->Form->hidden('id');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows'=>'3'));

echo $this->Form->input('Post.Category',array('label'=>'Categories <span class="star">*</span>','class'=>'form-control multiple-select', 'multiple'=>true));

echo $this->Form->end('Save Post');
 
 

View/Posts/index.ctp







<h1>Blog posts</h1>
<?php echo $this->Html->link('Add post', array('action' => 'add')); ?>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
        <th>Action</th>
    </tr>

    <?php if(!empty($posts)): ?>
        <?php foreach ($posts as $post): ?>
        <tr>
            <td><?php echo $post['Post']['id']; ?></td>
            <td>
                <?php echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
            </td>
            <td><?php echo $post['Post']['created']; ?></td>
            <td>
                <?php
                echo $this->Html->link('Edit', array('action' => '/edit', $post['Post']['id'])).' ' ;
                echo $this->Form->postLink('Delete', array('action' => '/delete', $post['Post']['id']), array('confirm' => 'Are you sure you want to delete this Post?'));
                ?>
            </td>
        </tr>
        <?php endforeach; ?>
    <?php endif; ?>
    <?php unset($post); ?>
</table>

View/Posts/view.ctp

<h1><?php echo h($post['Post']['title']); ?></h1>

<p><small>Created: <?php echo $post['Post']['created']; ?></small></p>

<p><?php echo h($post['Post']['body']); ?></p>

Model/Post.php

<?php

App::uses('AppModel','Model', 'CakeSession');

class Post extends AppModel {

    public $hasAndBelongsToMany = array(
        'Category' => array(
            'className' => 'Category',
            'joinTable' => 'category_posts',
            'foreignKey' => 'post_id',
            'associationForeignKey' => 'category_id',
            'unique' => 'keepExisting',
        )
    );

    public function beforeSave($options = array()) {

        foreach (array_keys($this->hasAndBelongsToMany) as $model){
            if(isset($this->data[$this->name][$model])){
                $this->data[$model][$model] = $this->data[$this->name][$model];
                unset($this->data[$this->name][$model]);
            }
        }

        return true;
    }
}