Cakephp HABTM协会保存不工作

时间:2013-09-03 07:53:09

标签: php cakephp has-and-belongs-to-many cakephp-appmodel

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'
                  )
              );
}

用户体验 向用户呈现项目,用户喜欢该项目,因此他将该项目保存到他的项目中 保存的项目。用户意图是成为此类项目的一部分。

程序员目标: 我打算在用户和项目之间使用一个连接表作为检索相关数据的方法。此连接表应用作两个模型之间的链接。用户发布的项目以及用户所属的项目(用户感兴趣并希望保存的其他项目,或者是其中的一部分)显然是多对多关系。

问题: 单击按钮(保存按钮)后,用户应该能够通过简单地更新连接表或添加关联来保存此项目,例如用户45与项目8900相关联。

当我尝试通过User模型更新连接表时,它不会保存数据。

1 个答案:

答案 0 :(得分:1)

每次我设置HABTM关系时,我都会为连接表创建一个模型(例如UsersProject),然后设置这样的关系:

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

然后你的saveProject函数实际上应该

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

当然,您还需要为连接关系创建适当的数据库表。

以下是在Cakephp 1.3中设置HABTM的参考资料。对不起,我没有2.x的经验,但我认为这是相同的概念。

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