Question

view.ctp

<?php
if($this->Session->check('Auth.User.id'))
{
    $userid = 
    $this->Session->read('Auth.User.id');
}
?>
<div class="post">

<h1>
    <?php  
    echo $project['Project']['title']; 
    ?>
</h1>

    <p>
        <?php 
        echo h($project['Project']['text']); 
        ?>
    </p>
    <div class="post-list">
          <ul >
        <li >
            <span class="basic-title">
                <?php
                echo
                __('Project status')
                ?>
            </span>
            :

            <?php 
              echo h($project['Project']['status']); 
            ?>
        </li>
        <li>
            <span class="basic-title">
                <?php
                echo
                __('Project team')
                ?>
            </span>
            :

            <?php 
              echo h($project['Project']['team']); 
              ?>
        </li>
        <li>
           <span class="basic-title">
                <?php
                echo
                __('Project fees')
                ?>
            </span>
            :

            <?php 
              echo h($project['Project']['fees']); 
              ?>
        </li>

         <li>
           <span class="basic-title">
                <?php
                echo
                __('Published on')
                ?>
            </span>
            :

            <?php 
            echo  
            $this->Time->nice($project['Project']['posted']); 
              ?>
        </li>

        <li>
           <span class="basic-title">
                <?php
                echo
                __('Author')
                ?>
            </span>
            :

            <?php 
            echo
            $this->Html->link(
                    $project['User']['username']
                    ,
                    array(
                        'controller' =>
                        'users',
                        'action' =>
                        'view',
                        $project['User']['id']
                    )
                    )

              ?>
        </li>
    </ul>
    </div>



</div>
<div class="actions">

    <ul class="inner-menu">
        <li>
                    <?php 
                    if(isset($user_id) && ($user_id == $project['Project']['user_id'])):
                    echo 
                    $this->Html->link(__('Edit Project'), 
                            array('action' => 'edit', 
                                $project['Project']['id'])); 
                    ?> 
                </li>
        <li>
                    <?php 
                    echo 
                    $this->Form->postLink(__('Delete Project'), 
                            array('action' => 'delete', 
                                $project['Project']['id']), 
                            null, 
                            __('Are you sure you want to delete # %s?', 
                                    $project['Project']['id']));

                    endif;
                    ?> 
                </li>



        <li>
                    <?php 
                    echo 
                    $this->Html->link(__('List Projects'), 
                            array('action' => 'index')); 
                    ?> 
                </li>
        <li>
                    <?php 
                    echo 
                    $this->Html->link(__('New Project'), 
                            array('action' => 'add')); 
                    ?> 
                </li>



    </ul>

  <?php
  echo
  $this->Form->create(
          'User',
          array(
              'url' =>
              array(
                  'controller' => 'users',
                  'action' => 'saveProject'
              )
          )
          );

  echo 
  $this->Form->input(
          'user_id',
          array(
              'type' => 'hidden',
              'value' => 
              $this->Session->read('Auth.User.id')
          )
          );

  echo 
  $this->Form->input(
          'project_id',
          array(
              'type' => 'hidden',
              'value' => 
              $project['Project']['id']
          )
          );

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


  ?>



</div>

UsersController.php

<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property RequestHandlerComponent $RequestHandler
 * @property SessionComponent $Session
 * @property AuthComponent $Auth
 */
class UsersController extends AppController {

/**
 * Helpers
 *
 * @var array
 */
    public $helpers = array('Text', 'Js', 'Time', 'Session');

/**
 * Components
 *
 * @var array
 */
    public $components = 
                array(
                    'RequestHandler', 
                    'Session', 
                    'Auth' =>
                    array(
                        'allowedActions' => 
                        array(
                            'add',
                            'index',
                            'view'
                        ),

                        'authenticate' =>
                        array(
                      'Form' => array(
                          'userModel' => 'User',
                          'fields' => array(
                              'username' => 'email',
                              'password' => 'password'
                                )
                            )
                        )

                    )

                    );

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->User->recursive = 1;
        $this->set('users', $this->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {


            if($this->Auth->loggedIn())
            {
                $this->redirect('users/index');
            }


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

                    $this->User->create();

                    if ($this->User->save($this->request->data)) 
                        {


                        $this->request->data['User']['id'] =
                                $this->User->id;


                        if($this->Auth->login($this->request->data['User']))
                        {

                            $this->Session->setFlash(__('The user has been saved'),
                            'standard');

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

            } 
                        else 
                        {
                            $this->Session->setFlash(__('The user could not be saved. Please, try again.'),
                            'standard');
            }
        }
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'),
                            'standard');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'),
                            'standard');
            }
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'),
                            'standard');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'),
                            'standard');
        $this->redirect(array('action' => 'index'));
    }

/**
 * admin_index method
 *
 * @return void
 */
    public function admin_index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_view($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
    }

/**
 * admin_add method
 *
 * @return void
 */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_edit($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
    }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
    public function admin_delete($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'),
                            'standard');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'),
                            'standard');
        $this->redirect(array('action' => 'index'));
    }





        public function login()
        {

            if($this->Auth->loggedIn())
            {
                $this->redirect('/users/index');
            }


            if($this->request->is('POST'))
            {
                if($this->Auth->login())
                {

                    //TODO: add last online data here

                    $this->Session->setFlash('You have been logged in',
                            'standard'
                            );
                    $this->redirect('/users/index');
                }
            }
        }



        public function logout()
        {

            $this->redirect($this->Auth->logout());

        }



        public function saveProject()
        {
            if($this->request->is('POST'))
            {
                if($this->User->save($this->data))
                {
                    $this->Session->setFlash(__('Saved'), 'standard');
                    $this->redirect($this->referer());
                }

            }
        }
}

User.php

<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 */
class User extends AppModel {




        public $hasOne =
                array(
                  'Client',
                  'Server',
                  'Framework'
                );

        public $hasMany =
                array(
                    'Project'                  
                );

        public $hasAndBelongsToMany =
                array(
                  'Project'
                );
/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                'message' => 'Please add your email',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),

                        'isUnique' => array(
                          'rule' => 'isUnique',
                          'message' => 'That email already exists'  
                        )
        ),
        'username' => array(
            'minlength' => array(
                'rule' => array('minlength', 4),
                'message' => 'This field must have at least 4 letters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'maxlength' => array(
                'rule' => array('maxlength', 40),
                'message' => 'This field can\'t have more than 30 letters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            )
        ),
        'password' => array(
            'minlength' => array(
                'rule' => array('minlength', 8),
                'message' => 'Password is too short, at least 8 letters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'maxlength' => array(
                'rule' => array('maxlength', 40),
                'message' => 'Password is too long, maximum 40 letters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'password-confirm' => array(
            'minlength' => array(
                'rule' => array('minlength', 8),
                'message' => 'Password is too short, at least 8 letters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'maxlength' => array(
                'rule' => array('maxlength', 40),
                'message' => 'Password is too long, maximum 40 letters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),


                        'matchPasswords'=>
                        array
                        (
                        'rule'=>'matchPasswords',
                        'message'=>
                        'Passwords must be identical'

                       )
        ),
    );




        public function matchPasswords()
          {
              if($this->data['User']['password'] 
                 == 
                 $this->data['User']['password-confirm'])
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }




          public function beforeSave() {


              if(
                      isset($this->data['User']['password']) 
                      && 
                      !empty($this->data['User']['password']))
              {
                  $this->data['User']['password'] =
                  AuthComponent::password($this->data['User']['password']);
              }



               if(
                       isset($this->data['User']['password-confirm']) 
                       && 
                       !empty($this->data['User']['password-confirm']))
              {
                  $this->data['User']['password-confirm'] =
                  AuthComponent::password($this->data['User']['password-confirm']);
              }





              return true;
          }
}

Project.php

<?php
App::uses('AppModel', 'Model');
/**
 * Project Model
 *
 * @property User $User
 */
class Project extends AppModel {

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'title';

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'title' => array(
            'minlength' => array(
                'rule' => array('minlength', 5),
                'message' => 'Title must be longer than 5 characters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'maxlength' => array(
                'rule' => array('maxlength', 100),
                'message' => 'Title must be shorter than 100 characters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'text' => array(
            'minlength' => array(
                'rule' => array('minlength', 20),
                'message' => 'Description must be at least 20 characters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'maxlength' => array(
                'rule' => array('maxlength', 3000),
                'message' => 'Description must be no more than 3000 characters',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'status' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'This field can\'t be empty',
                //'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(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );




      public $actsAs =
              array(
                  'Translate' =>
                  array(
                      'status',
                      'team'
                  )
              );
}

User experience: A user is presented with a project, user likes the project so he saves this project to his saved projects.The user intention is to become part of such project.

Programmer goal: I intend to use one join table between users and projects as a way to retrieve related data. This join table should be used as a link between both models. Projects posted by the user and as projects that the user belongs to (other projects the user is interested and wish to save or namely be a part of) obviously a many to many relationship.

Problem: Upon clicking a button (Save button) the user should be able to save this project by simply updating the join table or adding an association e.g User 45 is associated to project 8900.

When I tried to update the join table through the User model it won't save the data.

Was it helpful?

Solution

Everytime I've setup a HABTM relationship, I create a model for the join table (e.g. UsersProject) and then setup the relationship like this:

User hasMany UsersProject
UsersProject belongsTo User, Project
Project hasMany UsersProject.

Then your saveProject function should actually do

$this->User->UsersProject->save($this->data)

Of course you also need to have the appropriately created database table for the join relationship.

Here's a reference to setting up HABTM in Cakephp 1.3. Sorry I have no experience in 2.x but I assume it's the same concept.

http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#saving-related-model-data-habtm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top